BT

InfoQ Homepage News NUnit’s Action Attributes Simplify Unit Test Writing

NUnit’s Action Attributes Simplify Unit Test Writing

Bookmarks

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.

***** BankingTests.AccountTest.DepositFunds
Before via inheritance Case: AccountTest, from DepositFunds.
After via inheritance Case: AccountTest, from DepositFunds.
***** BankingTests.AccountTest.TransferFunds
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.

***** BankingTests.AccountTest.TransferWithInsufficientFunds
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.

Rate this Article

Adoption
Style

Hello stranger!

You need to Register an InfoQ account or or login to post comments. But there's so much more behind being registered.

Get the most out of the InfoQ experience.

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Community comments

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

BT

Is your profile up-to-date? Please take a moment to review and update.

Note: If updating/changing your email, a validation request will be sent

Company name:
Company role:
Company size:
Country/Zone:
State/Province/Region:
You will be sent an email to validate the new email address. This pop-up will close itself in a few moments.