MbUnit : RowTestAttribute
This page last changed on Jun 06, 2007 by vkreynin.
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 ):
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. [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 [RowTest] [Row(typeof(StringBuilder))] public void CreationOfInstancesTest(StringBuilder sb) { Assert.IsNotNull(sb, "A new StringBuilder object should have been created"); } |
Document generated by Confluence on Jun 11, 2007 11:56 |