NUnit supports a feature where you can specify a set of data inputs for a unit test to be run multiple times.
[RowTest]
[Row(1001,1,2,3)]
[Row(1,1001,2,3)]
[
You can create a base class with the test method and the parameters as virtual properties. When you inherit from this class you only need to override the properties with the desired values. Please see the sample code:
public class Operation
{
public static int Add(int x, int y)
{
return x + y;
}
}
[TestClass]
public class AddTests : WorkItemTest
{
protected virtual int First{get { return 0; }}
protected virtual int Second{get { return 0; }}
[TestInitialize]
public virtual void Init()
{
//Init code
}
[TestCleanup]
public virtual void Clean()
{
//Clean code
}
[TestMethod]
[Description("x+y = y+x")]
public virtual void Test_operation_commutativity()
{
Assert.AreEqual(Operation.Add(Second, First), Operation.Add(First, Second));
}
}
[TestClass]
public class AddPositiveTest : AddTests
{
protected override int First { get { return 1; } }
protected override int Second { get { return 2; } }
}
[TestClass]
public class AddNegativeTest : AddTests
{
protected override int First { get { return -1; } }
protected override int Second { get { return -2; } }
}