Last Updated: February 25, 2016
·
1.152K
· tuxxilla

NUnit's Action Attribute

Today I discovered NUnit's "Action Attribute". It really helps to clean up test code if you have multiple tests which share a common behavior.

Given you have integration tests which need to store some files to a temporary directory. Each of these tests needs to setup a temp directory and delete it during teardown. NUnit actions allow to seperate this setup/teardown behavior without the need for evil inheritance.

The following Gist shows how it is solved. Just implement ITempDirectoryTest in your test class. That's all (see DemoTest). The Attribute implementation is shown in TempDirectoryAwareActionAttribute.

View complete Gist

Further reading on NUnit Action Attributes