Mathias Brandewinder on .NET, F#, VSTO and Excel development, and quantitative analysis / machine learning.
by Mathias 17. May 2009 02:25

Another nice improvement coming with NUnit 2.5 is the mechanism for data-driven tests. NUnit 2.4.7 included an extension by andreas schlapsi which permitted to write row tests, using the [TestRow] attribute.

NUnit 2.5 eases the process with the [TestCase] attribute. Unlike [TestRow], the [TestCase] attribute is available within the NUnit.Framework namespace, and doesn’t require including additional references.

Why do Data-driven tests matter? They are not technically necessary: you can write the same tests as easily using the standard [Test] attribute. However, it comes handy when you are testing a feature where you want to verify the behavior for multiple combinations of input values. Using “classic” unit tests, you will end up duplicating test code, and you will have to find different name for tests method which are in essence the same test.

Using [TestCase] instead, here is how it looks. Suppose your class MyClass has a method “Divide” like this one:

public class MyClass
{
    public double Divide(double numerator, double denominator)
    {
        if (denominator == 0)
        {
            throw new DivideByZeroException("Cannot divide by zero."); 
        }
        return numerator / denominator;
    }
}

One way to test that feature would be with a test like that one:

[TestCase(2.5d, 2d, Result=1.25d)]
[TestCase(-2.5d, 1d, Result = -2.5d)]
public double ValidateDivision(double numerator, double denominator)
{
    var myClass = new MyClass();
    return myClass.Divide(numerator,denominator);
}

Each TestCase will match the arguments of the test method with the arguments provided in parenthesis (e.g. the first test will pass a numerator of 2.5 and a denominator of 2.0) and verify that the return value matches the value provided as “Result”.

One feature I really like in the new implementation is that it also supports exceptions testing. If a certain combination of input values is expected to throw an exception, it can also be expressed as a test case, without writing a dedicated test method:

[TestCase(2.5d, 2d, Result = 1.25d)]
[TestCase(-2.5d, 1d, Result = -2.5d)]
[TestCase(1d, 0d, ExpectedException = typeof(DivideByZeroException))]
public double ValidateDivision(double numerator, double denominator)
{
    var myClass = new MyClass();
    return myClass.Divide(numerator,denominator);
}

When you run this in the NUnit GUI, you will see something like this, displaying the “mother” test, and the result of each data set:

TestCase

As an aside, this post made me realize that dividing a double by zero was not throwing a “DivideByZeroException”, as I naively thought it would, but returns a double.PositiveInfinity (or NegativeInfinity, depending on the numerator). Goes to show that nothing beats writing unit tests when trying to understand a feature of a language!

Comments

1/4/2010 8:09:28 AM #

Data Analyst

nice article....thank for your information about Data driven tests with NUnit 2.5, i will try it

Data Analyst United States | Reply

5/4/2010 5:37:26 PM #

pingback

Pingback from endyear2012.com

car insurance cheap quote «  End Year 2012

endyear2012.com | Reply

5/20/2010 8:20:27 AM #

pingback

Pingback from 241.dlmreza.net

Chevelle Radiator Iron Steel, Chevelle Interior

241.dlmreza.net | Reply

3/1/2011 6:52:21 AM #

trackback

play slots online

play slots online | Reply

3/29/2011 4:32:49 PM #

trackback

No Tolerance for NUnit Data-Driven tests?

No Tolerance for NUnit Data-Driven tests?

Clear Lines Blog | Reply

Add comment




  Country flag

biuquote
  • Comment
  • Preview
Loading



Comments

Comment RSS