NUnit’s Action Attributes Simplify Unit Test Writing
The latest release of NUnit showcases Action Attributes, a feature which enables the orchestration of test actions across suites, tests, and test cases. Developers can arrange the execution of setup, teardown, and other testing side-effects by applying Action Attributes, which encapsulate test actions, to classes, interfaces, methods, and assemblies within their test projects.
As a given test is ran, any Action Attributes associated with the test will be called twice in order of their definition, once for the BeforeTest event and again for the AfterTest event. This pattern gives developers discrete control over how their tests will behave both during setup and teardown.
In order to make use of Action Attributes, a developer would need to first define their new attribute and either implement the ITestAction interface while inheriting from Attribute, or inherit directly from TestActionAttribute.
The following code demonstrates implementing an Action Attribute by implementing the ITestAction interface.
Whereas the following code inherits from TestActionAttribute in order to achieve a similar effect.
Once the Action Attribute has been created, it can be applied as any normal .NET attribute to methods, classes, interfaces, or assemblies within a test project.
The output from NUnit reflects the composition of the Action Attributes.
Before via inheritance Case: AccountTest, from DepositFunds.
After via inheritance Case: AccountTest, from DepositFunds.
Before via Interface Case: AccountTest, from TransferFunds.
After via Interface Case: AccountTest, from TransferFunds.
Multiple Action Attributes can be applied to a single target. The Action Attributes will execute from right to left if defined within the same bracket or from bottom to top if defined independently.
When using this composition of Action Attributes, NUnit is able to execute each one within the same test.
Before via Interface Case: AccountTest, from TransferWithInsufficientFunds.
Before via inheritance Case: AccountTest, from TransferWithInsufficientFunds.
After via inheritance Case: AccountTest, from TransferWithInsufficientFunds.
After via Interface Case: AccountTest, from TransferWithInsufficientFunds.
Action Attributes offer the flexibility of reusing multiple aspects of functionality applied to unit tests. Composing the setup and behavior of unit tests can dramatically increase the productivity and effectiveness of the average tester, but it can also hide important testing logic from developers maintaining these tests and could make unit tests layered and obtuse if not used carefully.
In addition to Action Attributes, NUnit shipped with a total of 90 bug fixes and several other changes to its framework. The remainder of this release deals with ending its support for older versions of NUnit and .NET. As of the 2.6 release, NUnit will no longer support conventions introduced in its earliest versions and only .NET 2.0 and greater will be supported without the need for an extra download.
Ralph Winzinger Nov 25, 2014