Question

There are 2 tests that take the same list of servers in using the Row() attribute

Is there a way to avoid duplicating all the Row attributes on both tests?

[RowTest]
[Row("server1")]
[Row("server2")]
[Row("server3")]
public void Test1(string server)
{
    //try connecting on port
}

[RowTest]
[Row("server1")]
[Row("server2")]
[Row("server3")]
public void Test2(string server)
{
   //ping server
}
Was it helpful?

Solution

Yes. You can define a source for the test cases. The source can be either a method, field or a property, may be static or instance. The requirement is that the source should return an IEnumberable or another type that implements it. Of course the sources are reusable for multiple test methods which is your requirement.

You indicate the source for test cases by decorating the method with the TestCaseSourceAttribute. The snippet below is an example from the link I provided.

static int[] EvenNumbers = new int[] { 2, 4, 6, 8 };

[Test, TestCaseSource("EvenNumbers")]
public void TestMethod(int num)
{
    Assert.IsTrue( num % 2 == 0 );
}

Also take into consideration that in the latest verion of NUnit, the is no Row or RowTest attribute, for more information visit this question: What happended to nunit extensions/rowtest?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top