My test method looks like this:
public static List<Something> Generator() {
return A.GenerateObjects();
}
[Test, TestCaseSource(nameof(Generator))]
public void DoSomething(Something abc) {/*do something*/}
This code works very well and generates for each object in the list an unit case.
I want to include another parameter in the method like:
public void DoSomething(Something abc, string def)
I've tried it with these lines but it didn't work:
public static object[] Case =
{
new object[]
{
A.GenerateObjects(),
someStrings
}
};
Maybe iterate the list with an loop function instead of invoking the method (GenerateObjects()) directly? I also don't understand how Nunit can recognize the objects from the list directly with only TestCaseSource(nameof(Generator))
Thanks in advance!
You can return an IEnumerable of TestCaseData like this:
public static IEnumerable<TestCaseData> Generator()
{
yield return new TestCaseData(new Something { SomeValue = "Hi" }, "Foo").SetName("FirstTest");
yield return new TestCaseData(new Something { SomeValue = "Bye" }, "Bar").SetName("SecondTest");
}
[Test]
[TestCaseSource(nameof(Generator))]
public void DoSomething(Something abc, string def)
{
Console.WriteLine($"{abc.SomeValue},{def}");
}
The SetName is optional, just if you want a more meaningful name than the default one it makes up.
I also don't understand how Nunit can recognize the objects from the list directly with only TestCaseSource(nameof(Generator))
Nunit notices the TestCaseSource attribute on the test method, and then uses reflection to invoke the "Generator" method. (Nameof is just sugar, the compiler substitutes it with the actual name when you build it). Every TestCaseData object that is returned is another test case. In my example above the tests will be run twice. FirstTest will have a Something instance where SomeValue is set to Hi and a def string of Foo. SecondTest will have a Something instance where SomeValue is set to Bye and a def string of Bar.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With