Wednesday, October 20, 2004

This new functionality enables you to specify a tabular list of test cases that are to be feeded to the test method. Let me illustrate that with the floating point division testing (as in the FIT framework Simple Example ):

numerator denominator quotient()
1000 10 100.0000
-1000 10 -100.0000
1000 7 142.85715
1000 .00001 100000000
4195835 3145729 1.3338196

You can now translate this directly to C# in MbUnit using the RowTestAttribute and the RowAttribute:

[TestFixture]
public class DivisionFixture
{
    [RowTest]
    [Row(1000,10,100.0000)]
    [Row(-1000,10,-100.0000)]
    [Row(1000,7,142.85715)]
    [Row(1000,0.00001,100000000)]
    [Row(4195835,3145729,1.3338196)]
    public void DivTest(double numerator, double denominator, double result)
    {
        Assert.AreEqual(result, numerator / denominator, 0.00001 );
    }
}

Of course, these tests are not very well targeted because we do not test the “special” floating point values such as 1,0,double.MaxValue, double.MinValue, NaN but you get the picture.

What if a test should throw ? In that case, you can specify the exception type as an additional parameter in the RowAttribute constructor:

    [Row(1,0,0, ExpectedException = typeof(ArithmeticException))]
    public void DivTest(double numerator, double denominator, double result)
    {...}

The final output of the tests is as follows where you can see that 5 tests (one per row) were generated and executed.

Info: Found 5 tests
Info: [assembly-setup] success
Info: [success] RowTestDemo.DivTest(0)
Info: [success] RowTestDemo.DivTest(1)
Info: [success] RowTestDemo.DivTest(2)
Info: [success] RowTestDemo.DivTest(3)
Info: [success] RowTestDemo.DivTest(4)
Info: [assembly-teardown] success
Info: [reports] generating HTML report
posted on Wednesday, October 20, 2004 9:30:00 PM UTC  #    Comments [7]
Tracked by:
"tub refinishing" (online) [Trackback]
"navigatore" (online) [Trackback]
Monday, June 06, 2005 4:54:43 PM UTC
That is so freakin' cool. It's something I'm always writing code for.
nospamplease75@yahoo.com (Haacked)
Monday, June 06, 2005 4:54:44 PM UTC
you've been HAACKED
Monday, June 06, 2005 4:54:46 PM UTC
you've been HAACKED
Monday, June 06, 2005 4:54:47 PM UTC
Any way you could print out in the error report the parameters which generated the error? Just counting down in numerical order doesn't seem to always work.
<br>
John Melville
Monday, June 06, 2005 4:54:47 PM UTC
I'll look into that. Could you open an issue on <a title="MbUnit, Generating Unit Testing and Model Based Testing Framework for .NET Framework" href="http://mbunit.tigris.org" target="_blank">MbUnit</a>.tigris.org, it would be easier for me to track such feature request.
Jonathan de Halleux
Monday, June 06, 2005 4:54:47 PM UTC
I've added the actually row arguments in the test case name. Should be available in the next automated build.
Jonathan de Halleux
Monday, June 06, 2005 4:54:47 PM UTC
I finally got my copy of Visual Studio 2005 in the mail. I'd have it sooner if it weren't for the fact that microsofts prices are just a little bit out of my range. So I ordered the beta on cd and was pleasantly surprised when I also received a year trial
Test it 'till it hurts
Comments are closed.