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+

NUnit Comparison Assertions

The sixth part of the Automated Unit Testing tutorial continues the description of the assertion commands provided by the NUnit framework. This instalment looks at comparison asserts, which ensure that a result is greater or less than an expected value.

Comparison Asserts

The comparison asserts allow you to compare two values in your tests and assert that one is greater than or less than the other, providing similar functionality to conditions that contain relational operators. As with the equality assertions, if the condition you specify is not met the test is reported as a failure in the NUnit test runner.

In this article we will create some tests for a sample method. The method generates strings that contain a repeating character and are of a length that is randomly generated according to provided minimum and maximum values. The code is as follows:

public class StringGenerator
{
    Random _random = new Random();

    public string Generate(char character, int minLength, int maxLength)
    {
        int length = _random.Next(minLength, maxLength + 1);
        return new string(character, length);
    }
}

Assert.Less

The first of the comparison assertions is provided by the Less method. This is a static method of the Assert class. You should use Assert.Less when you do not know the exact expected result of an operation but want to ensure that a generated result is less than an expected value. The assertion can work with numeric data types and any other type that implements the IComparable interface.

The example below calls the Generate method with a length range of six to ten characters. The assertion ensures that the resultant string has fewer than eleven characters. Note the order of the parameters; the statement Assert.Less(a,b) ensures that a is less than b. This order pattern is the same for all of the comparison asserts.

[TestFixture]
public class AdderTests
{
    StringGenerator _generator = new StringGenerator();
    string _result;

    [SetUp]
    public void SetUp()
    {
        _result = _generator.Generate('*', 6, 10);
    }

    [Test]
    public void ResultantStringIsLessThan11Chars()
    {
        Assert.Less(_result.Length, 11);
    }
}

Assert.Greater

The Greater assert is the opposite of Less. This method asserts that the value of the first argument is larger than the second. We can use this to ensure that the string's length is always more than five characters by adding the following test to the fixture:

[Test]
public void ResultantStringIsMoreThan5Chars()
{
    Assert.Greater(_result.Length, 5);
}

Assert.LessOrEqual

Earlier we used the Assert.Less method to test that length of the generated string was fewer than eleven characters. As the range of possible lengths is specified by inclusive arguments, it would be more intuitive to use check that the generated string is at most as long as the upper value, rather than less than the provided argument plus one. To do so, we can use the LessOrEqual assertion instead:

[Test]
public void ResultantStringIsLessThanOrEqualTo10Chars()
{
    Assert.LessOrEqual(_result.Length, 10);
}

Assert.GreaterOrEqual

The final assert in this article is GreaterOrEqual. As the name suggests, this confirms that one value is greater than or equal to another. We will use this to check that the strings length is above or equal to the lower limit:

[Test]
public void ResultantStringIsMoreThanOrEqualTo6Chars()
{
    Assert.GreaterOrEqual(_result.Length, 6);
}
9 April 2011