Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to use NUnit TestCase attribute with optional arguments

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?

like image 818
ranjez Avatar asked Mar 05 '14 11:03

ranjez


People also ask

What are optional arguments in C#?

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.

Which attribute is used when there is a requirement for parameterized tests?

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.

Which of the named parameters are supported by the TestCase attribute?

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.

How do I use Testcasesource in NUnit?

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.


1 Answers

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");          
}
like image 153
Peter Avatar answered Sep 19 '22 22:09

Peter