Values
The ValuesAttribute
is used to specify a set of values to be provided
for an individual parameter of a parameterized test method. Since
NUnit combines the data provided for each parameter into a set of
test cases, data must be provided for all parameters if it is
provided for any of them.
By default, NUnit creates test cases from all possible combinations of the data values provided on parameters - the combinatorial approach. This default may be modified by use of specific attributes on the test method itself.
Example
[Test]
public void ValuesAttribute_BasicExample([Values(1, 2, 3)] int x, [Values("A", "B")] string s)
{
Assert.That(x, Is.GreaterThan(0));
Assert.That(s, Is.Not.Null);
}
The above test will be executed six times, as follows:
MyTest(1, "A")
MyTest(1, "B")
MyTest(2, "A")
MyTest(2, "B")
MyTest(3, "A")
MyTest(3, "B")
Values with Enum or Boolean
The values attribute works in a special way with Enums and Boolean parameters.
When used without any arguments, the [Values] attribute on an enum parameter will automatically include all possible values of the enumeration.
public enum MyEnumType
{
Value1,
Value2,
Value3
}
[Test]
public void ValuesAttribute_EnumExample([Values] MyEnumType myEnumArgument)
{
Assert.That(myEnumArgument, Is.TypeOf<MyEnumType>());
}
There is the same support for Boolean values. Add the [Values] attribute to a bool and the method will be run with true and false.
[Test]
public void ValuesAttribute_BoolExample([Values] bool value)
{
// This test will run twice: once with true, once with false
Assert.That(value is true or false);
}