Mike Gunderloy has posted a nice introductory article on using TD.NET and MbUnit in Visual Studio. Check it out at http://www.developer.com/net/net/article.php/3453121
This article is really about TD.NET as Mike emphasis on one of the zillions of feature of TD.NET. There are a lot of very nice feature in TD.NET, just take a moment to discover them.
Going beyond Mike sample with MbUnit
Mike illustrates the article with a little class that stores an integer and returns information such as IsPositive, etc... In the article, Mike uses the canonical fixture. Let's if we can do better. (disclaimer: my VB is really bad)
RowFixture
Typically, you want to test this for 'interresting' values such 0,-1,1,MaxInt,etc... This example really fits well into the example of the RowFixture:
Imports System
Imports MbUnit.Core.Framework
Imports MbUnit.Framework
<TestFixture()> _
Public Class NumberTests
...
<RowTest()> _
<Row(5,FALSE)> _
<Row(8,TRUE)> _
Public Sub TestIsEven(int value, Boolean isEven)
' Make sure even numbers are property identified
Dim num As New Number
num.Value = value;
Assert.AreEqual(isEven, num.IsEven)
End Sub
End Class
This will create a test case for each RowAttribute. Better readibabilty, better test separation. Now can we do better ?
Combinatorial test
Let's try another approach. We could create a table of 'interresting' number with their properties (is positive, is even). Once this is done, we can feed this table to combinatorial tests.
<TestFixture()> _
Public Class NumberTests
Public Class NumberRow
Public int Value
Public bool IsPositive
Public bool IsEven
Public New(int value, bool isPositive, bool isEven)
Me.Value = value
Me.IsPositive = IsPositive
Me.IsEven = isEven
End New
End Class
<Factory()> _
Public Function Numbers() as Array Of NumberRow
Dim numbers As New Array Of NumberRow[4];
numbers[0] = New NumberRow(-1, False, False)
numbers[1] = New NumberRow(0, False, True)
numbers[2] = New NumberRow(1, True, False)
numbers[3] = New NumberRow(2, True, True)
Return numbers
End Function
<CombinatorialTest()> _
Public Sub TestIsEven(<UsingFactories("NUMBERS")> _ NumberRow row)
' Make sure even numbers are property identified
Dim num As New Number
num.Value = row.Value;
Assert.AreEqual(row.IsEven, num.IsEven)
End Sub
End Class
This will create 4 test cases, one per element of the array returned by Numbers. Moreover, we could reuse Numbers in another test to verify IsPositive.
[Update]
If you plan to reuse the factory, you can extract it into a separate class and pass the facotry type of the UsingFactories parameter.
Public Class NumberRow
Public int Value
Public bool IsPositive
Public bool IsEven
Public New(int value, bool isPositive, bool isEven)
Me.Value = value
Me.IsPositive = IsPositive
Me.IsEven = isEven
End New
End Class
Public Class NumberFactory
<Factory()> _
Public Function Numbers() as Array Of NumberRow
Dim numbers As New Array Of NumberRow[4];
numbers[0] = New NumberRow(-1, False, False)
numbers[1] = New NumberRow(0, False, True)
numbers[2] = New NumberRow(1, True, False)
numbers[3] = New NumberRow(2, True, True)
Return numbers
End Function
End Class
<TestFixture()> _
Public Class NumberTests
<CombinatorialTest()> _
Public Sub TestIsEven(<UsingFactories(CType(NumberFactory))> _ NumberRow row)
' Make sure even numbers are property identified
Dim num As New Number
num.Value = row.Value;
Assert.AreEqual(row.IsEven, num.IsEven)
End Sub
End Class