NUnit attributes


This is one of those entries for something which should be easy to remember, but, alas, I constantly forget: which NUnit attributes should I decorate my unit test classes with.

So, here is a cheat-sheet for myself. Perhaps, someday, I may expand this entry into a full reference.

[TestFixture]
public class ObjectUnitTests
{
    [TestFixtureSetUp][OneTimeSetup]
    public void BeforeAnyTesting() {...}

    [SetUp]
    public void BeforeEachTest() {...}

    [Test]
    [Category("AlwaysRuns")]
    public void AUnitTest() 
    {
        // Arrange
        ...
    
        // Act
        ...

        // Assert
        ...
    }

    [Ignore]
    [Category("NotAlwaysRuns")]
    public void DontRunThisTestEver() {...}

    [Explicit]
    [Category("NotAlwaysRuns")]
    public void UserMustManuallyRequestThisTest() {...}

    [TearDown]
    public void AfterEachTest() {...}

    [TestFixtureTearDown][OneTimeTearDown]
    public void AfterAllTesting() {...}
}

Note: As of NUnit 3, [TestFixtureSetup] and [TestFixtureTearDown] are replaced by [OneTimeSetup] and [OneTimeTearDown] – which does make it a little simpler to remember the usages.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s