NUnit 3.0 - Values Attribute for Enums

2014, Nov 07    

To celebrate the second alpha release of NUnit 3.0, I am going to write a few posts on some of the new features that we've added.

Today's feature is a simple one, but one of my favorites. With older versions of NUnit, if you wanted to run a test against every value of an enum, you would either have to write code to iterate through the values, or add them as TestCases and risk missing values added to the enum later. With the 3.0 release, you can now write a test method like the following which will be run with every enum value. The [Values] attribute on an enum parameter will automatically expand to all possible Enum values.


[Test]
public void MyEnumTest([Values]MyEnumType myEnumArgument)
{
    //...
}

It is probably less useful, but we also added 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 MyBoolTest([Values]bool value)
{
    //...
}