Dashboard > MbUnit > ... > MbUnit Home > RowTestAttribute
  MbUnit Log In View a printable version of the current page.  
  RowTestAttribute
Added by Jonathan de Halleux, last edited by Vadim Kreynin on Jun 06, 2007  (view change)
Labels: 
(None)

This new functionality enables you to specify a tabular list of test cases that are to be fed to the test method.

Let us illustrate this 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]:

Unable to find source-code formatter for language: cs. Available languages are: actionscript, html, java, javascript, none, sql, xhtml, xml
[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 );
    }
}

The final output of the tests are 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

What if a test should throw ?

In that case, you can specify the exception type as an additional parameter in the RowAttribute constructor:

Unable to find source-code formatter for language: cs. Available languages are: actionscript, html, java, javascript, none, sql, xhtml, xml
[RowTest] [Row(1,0,0, ExpectedException = typeof(ArithmeticException))]
public void DivTestFail(int numerator, int denominator, int result)
{...}

How many parameters can I bind to each row ?

Beginning with MbUnit 2.3 RC2, the RowAttribute constructor accepts an unlimited number of parameters using the "params object[] args" notation. So you can bind zero or more parameters to a row by passing them in as an array literal or simply enumerating them. Previous versions required at least two parameters to be specified for each row and only supported up to eight in total.

The following attribute declarations are equivalent.

Unable to find source-code formatter for language: cs. Available languages are: actionscript, html, java, javascript, none, sql, xhtml, xml
[Row(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)]
[Row(new object[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 })]

Decimal parameters in Row attribute.

Beginning with MbUint 2.4 RTM, it possible to use decimal in RowAttribute constructor.

[RowTest]
[Row(10.2, 2.5)]
public void DecimalTest(decimal x, decimal y)
{
    Assert.AreEqual(12.7, x + y);
}

It also possible to express decimal parameter as string in RowAttribute.  The conversion is culture invariant. That means that the decimal separator will always be a dot.
The code bellow is equivalent to the code above.

[RowTest]
[Row("10.2", "2.5")]
public void DecimalTest(decimal x, decimal y)
{
    Assert.AreEqual(12.7, x + y);
}

If a decimal parameter is cannot be represented in 64 bit (15-16 digits), then you must put this parameter between double quotes (").

[RowTest]
[Row(21.5, 5.21, "4.1266794625719769673704414587″)]
public void Div(decimal x, decimal y, decimal result)
{
  Assert.AreEqual(result, x / y);
}

DateTime parameters in Row attribute.

 Beginning with MbUint 2.4 RTM, it possible to use DateTime parameters in RowAttribute constructor.

[RowTest]
[Row("2006-05-04 12:03:05")]
public void TestDateTimeConversion(DateTime dt)
{
   DateTime theDate = new DateTime(2006, 5, 4, 12, 3, 5);
   Assert.AreEqual(dt, theDate);
} 

The conversion is culture invariant. That means that the ate format will always "yyyy-mm-dd hh:mm:ss". If it weren't that way, you'd end up having tests that fail when running under different cultures.U

 Using null's in C# 2003

 

[RowTest]
[Row(SpecialValue.Null)]
public void SpecialValue_Null(string x)
{
    Assert.IsNull(x);
}

 Using reference types

 
 Beginning with MbUnit 2.4 RTM, you can instantiate reference types when the test method expects them, provided there is a public parameterless constructor for the type.

[RowTest]
[Row(typeof(StringBuilder))]
public void CreationOfInstancesTest(StringBuilder sb)
{
    Assert.IsNotNull(sb, "A new StringBuilder object should have been created");
}

 
 

Site powered by a free Open Source Project / Non-profit License (more) of Confluence - the Enterprise wiki.
Learn more or evaluate Confluence for your organisation.
Powered by Atlassian Confluence, the Enterprise Wiki. (Version: 2.2.9 Build:#527 Sep 07, 2006) - Bug/feature request - Contact Administrators