BlackWaspTM

This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

Testing
.NET 1.1+

Creating Unit Tests

The third part of the Automated Unit Testing tutorial examines the construction of unit tests using the NUnit testing framework. This article describes the use of the attributes that determine how the test runner executes unit test code.

SetUp and TearDown

The example tests above include a single assertion each. In real projects it is common to perform several assertions for each tested operation. It is possible to include multiple Assert commands in a single test method, as in the example below:

[TestFixture]
public class ArrangeActAssert
{
    [Test]
    public void CombiningWordsWorksCorrectly()
    {
        // Arrange
        string word1 = "Hello";
        string word2 = "World";

        // Act
        string phrase = string.Format("{0} {1}", word1, word2);

        // Assert
        Assert.AreEqual("Hello World", phrase);
        Assert.AreEqual(11, phrase.Length);
        Assert.AreEqual(" ", phrase.Substring(5, 1));
    }
}

Adding multiple assertions to a single test method has a key drawback. If the first Assert command causes a failure, all subsequent assertions are ignored by the test runner. When you fix the problem that causes the first failure you may discover that the second assertion highlights a further problem. A better solution is to have only one assertion per test method. This guarantees that every assertion is tested and all problems are displayed immediately.

You could separate the previous example's assertions into three test methods using only the TestFixture and Test attributes. However, this would mean that the Arrange and Act steps would need to be duplicated for each test. This duplication lowers the maintainability of the tests should either step need to be changed. The problem is overcome using the SetUp attribute.

Each test fixture may include one method that is used to set up your tests. The method is decorated with the SetUp attribute to tell the NUnit test runner that it should be executed immediately before each test. The method is generally used to hold the code for the Arrange and Act steps of the AAA pattern. The test methods become focussed upon executing assertions. NB: The SetUp method must be public, have no parameters and return void.

To show the operation of the SetUp attribute, try running the tests shown below, which are the equivalent of the previous sample. To allow the tests to access the data that needs to be checked, the two word variables and the phrase variable have been promoted to the class level. The Assert and Act steps have been moved to the SetUp method, which will be executed three times, once for each test.

[TestFixture]
public class WhenCombiningWordsWithStringFormat
{
    string _word1, _word2, _phrase;

    [SetUp]
    public void SetUp()
    {
        _word1 = "Hello";
        _word2 = "World";

        _phrase = string.Format("{0} {1}", _word1, _word2);
    }

    [Test]
    public void TheTwoWordsAreCombined()
    {
        Assert.AreEqual("Hello World", _phrase);
    }

    [Test]
    public void ThePhraseIsTheCorrectLength()
    {
        Assert.AreEqual(11, _phrase.Length);
    }

    [Test]
    public void TheSeparatorIsASpace()
    {
        Assert.AreEqual(" ", _phrase.Substring(5, 1));
    }
}

Some testing may require that an action be performed after each test, whether successful or not. This is often the case with integration tests where a database connection or a file needs to be closed. Some clean up may also be required with unit tests.

You can create a method that is executed after each test by applying the TearDown attribute. As with the SetUp attribute, TearDown must be applied to a public method with no parameters and a void return value.

[TearDown]
public void TearDown()
{
    _someObject.Dispose();
}
23 March 2011