I'm trying to run some test cases but I need to make one of the arguments optional.
I've tried the following but NUnit ignores the tests and prints the following "ignored: Wrong number of arguments provided"
[TestCase(Result = "optional")]
[TestCase("set", Result = "set")]
public string MyTest(string optional = "optional")
{
return optional;
}
Is it possible to run test cases with optional arguments?
Optional arguments enable you to omit arguments for some parameters. Both techniques can be used with methods, indexers, constructors, and delegates. When you use named and optional arguments, the arguments are evaluated in the order in which they appear in the argument list, not the parameter list.
TestCase Attribute. The TestCase attribute in NUnit marks a method with parameters as a test method. It also provides the inline data that needs to be used when that particular method is invoked.
TestCaseAttribute supports a number of additional named parameters: Author sets the author of the test. Category provides a comma-delimited list of categories for this test. Description sets the description property of the test.
TestCaseSourceAttribute is used on a parameterized test method to identify the source from which the required arguments will be provided. The attribute additionally identifies the method as a test method. The data is kept separate from the test itself and may be used by multiple test methods.
Just make 2 test in this case, optional paramerters are not supported in nunit:
[TestCase("set", Result = "set")]
public string MyTest(string optional)
{
return optional;
}
[TestCase(Result = "optional")]
public string MyTest()
{
return MyTest("optional");
}
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