Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save matthewkastor/9e70f6809276450d1c71 to your computer and use it in GitHub Desktop.
Save matthewkastor/9e70f6809276450d1c71 to your computer and use it in GitHub Desktop.
Snippets of code for creating test spec objects, but still having NUnit list each method with it's args as an individual test in the tree
// NUnit can use a list of "TestParameters", but then it
// doesn't populate the list of tests with the method(parameter, parameter)
// item in the list. So, instead, I made a class so I could
// create the TestParameters object in queries, assign values
// to named properties, and when I need them in a list of
// object[] for NUnit, I just convert each one by calling it's
// ToObject() method.
/// <summary>
/// Structure of test parameters
/// </summary>
public class TestParameters
{
public string Stuff;
public string Thing;
/// <summary>
/// The arguments array for NUnit to pass to the test method
/// </summary>
public object[] ToObject() {
return new object[] {
Stuff, Thing
};
}
}
/// <summary>
/// Making a list of all possible test spec combinations
/// </summary>
public static List<TestParameters> Foo(){
from blah in Blah
select new TestParameters() {
Stuff = blah,
Thing = "thing"
}
};
/// <summary>
/// Filtering specs to just the ones you want
/// </summary>
public static List<object[]> Bar {
get {
return Foo().Where(f => f.Baz.ToLower() == "awesome")
.Select(tp => tp.ToObject());
}
}
/// <summary>
/// Tests the thing all kinds of ways
/// </summary>
[Test]
[TestCaseSource("Bar")]
[Category("All Kinds Of Tests on the thing")]
public void CanDoStuffToThings(string stuff, string thing)
{
var did = System["stuff"].do(thing);
Assert.IsTrue(did, "stuff was not done to the thing");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment