Skip to content

Instantly share code, notes, and snippets.

@matthewkastor
Created January 14, 2016 20:08
Show Gist options
  • Save matthewkastor/62b55dc9143cb583ed39 to your computer and use it in GitHub Desktop.
Save matthewkastor/62b55dc9143cb583ed39 to your computer and use it in GitHub Desktop.
Combining lists of values into parameterized test data (specs) for NUnit
using System.Collections.Generic;
using System.Linq;
using NUnit.Framework;
namespace NUnitParameterizedTestsExample
{
/// <summary>
/// Parameterize all the tests
/// </summary>
[TestFixture]
[Timeout(120000)]
[Category("AllTheTestsExample")]
public class AllTheTestsExample
{
private List<string> SalesDocumentTypes
{
get
{
return new List<string> {
"Estimate",
"Order",
"Invoice",
"Refund",
"Return",
"SalesReceipt"
};
}
set { throw new System.NotImplementedException(); }
}
private List<string> PostableSalesDocumentTypes
{
get
{
return new List<string> {
"Invoice",
"Refund",
"Return",
"SalesReceipt"
};
}
set { throw new System.NotImplementedException(); }
}
private List<string> DefaultItemNumbers
{
get
{
return new List<string> {
"SerialTrackedItem",
"LotTrackedItem",
"NotTrackedItem"
};
}
set { throw new System.NotImplementedException(); }
}
private readonly List<string> _customerOptions = new List<string> {
"AddCustomer",
"NoCustomer"
};
private readonly List<string> _lineItemOptions = new List<string> {
"AddItem",
"DeleteItem",
"NoItems"
};
private readonly List<string> _documentAction = new List<string> {
"Post",
"Void",
"CreateThenClose"
};
private List<object> TestParameters
{
get
{
//with items
List<object[]> output = (from customerOption in _customerOptions
from lineItemOption in _lineItemOptions
where lineItemOption != "NoItems"
from salesDocType in SalesDocumentTypes
from item in DefaultItemNumbers
from documentAction in _documentAction
where
((documentAction == "Post")
&& PostableSalesDocumentTypes.Contains(
salesDocType))
|| documentAction != "Post"
select
new object[] {
salesDocType, documentAction, customerOption
, lineItemOption, item
}).ToList();
//without items
output.AddRange(
(from customerOption in _customerOptions
from lineItemOption in _lineItemOptions
where lineItemOption == "NoItems"
from salesDocType in SalesDocumentTypes
from documentAction in _documentAction
where
((documentAction != "Post")
&& !PostableSalesDocumentTypes.Contains(salesDocType))
|| documentAction != "Post"
select
new object[] {
salesDocType, documentAction, customerOption, lineItemOption,
null
}).ToList());
// filter out tests to post docs that can't be posted
foreach (var o in
output.Where(
o =>
(string) o[1] == "Post"
&& !PostableSalesDocumentTypes.Contains((string) o[0])))
{
output.Remove(o);
}
return output.Cast<object>()
.ToList();
}
}
private void DoTest(
string salesDocumentType,
string documentAction,
string customerOption,
string lineItemOption,
string itemNumber = null)
{
// automate UI for creating sales documents:
// 1. with or without customer assignment,
// 2. with or without line items,
// 3. delete line items after creating, or leave them behind
// 4. close, void, or post (if possible)
Assert.Fail("This test is not finished.");
}
/// <summary>
/// Verifies that the test can be done
/// </summary>
/// <param name="salesDocumentType">kind of document</param>
/// <param name="documentAction">do this with document</param>
/// <param name="customerOption">to have customer, or no</param>
/// <param name="lineItemOption">to have, edit, or delete line item</param>
/// <param name="itemNumber">item number to use</param>
[Test]
[TestCaseSource("TestParameters")]
[Category("Functional Tests")]
public void CanDoTest(
string salesDocumentType,
string documentAction,
string customerOption,
string lineItemOption,
string itemNumber)
{
DoTest(
salesDocumentType,
documentAction,
customerOption,
lineItemOption,
itemNumber);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment