September 30, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Frictionless Testing with TestDriven.NET

  • December 30, 2004
  • By Mike Gunderloy
  • Send Email »
  • More Articles »

Making Things Work

Now that you know for sure that the code in Number.vb isn't working, it's time to fix it. Here's a revised version of that class:


Public Class Number

    Public Sub New()

    End Sub

    Private _value As Integer

    Public Property Value() As Integer
        Get
            Return _value
        End Get
        Set(ByVal Value As Integer)
            _value = Value
        End Set
    End Property

End Class

Now if you run the unit test, you'll find that the output window reads:


1 succeeded, 0 failed, 0 skipped, took 0.00 seconds.

The MbUnit report turns a soothing, happy green with passed tests, too. With this little bit of functionality working, you can go on to write more code. But remember: write tests first, then write code. After a few iterations of this procedure, you might end up with a Number class that looks like this:


Public Class Number

    Public Sub New()

    End Sub

    Private _value As Integer

    Public Property Value() As Integer
        Get
            Return _value
        End Get
        Set(ByVal Value As Integer)
            _value = Value
        End Set
    End Property

    Public ReadOnly Property IsEven() As Boolean
        ' Returns true if the number is even
        Get
            Return _value Mod 2 = 0
        End Get
    End Property

    Public ReadOnly Property IsOdd() As Boolean
        ' Returns true if the number is odd
        Get
            Return _value Mod 2 = 1
        End Get 
    End Property

    Public ReadOnly Property IsPositive() As Boolean
        ' Returns true if the number is positive
        Get
            Return _value > 0
        End Get
    End Property
End Class

And here's the corresponding test class:


Imports System
Imports MbUnit.Core.Framework
Imports MbUnit.Framework

 _
Public Class NumberTests

     _
    Public Sub TestValue()
        ' Make sure we can store and retrieve a value
        Dim num As New Number
        num.Value = 5
        Assert.AreEqual(num.Value, 5)
    End Sub

     _
    Public Sub TestIsEven()
        ' Make sure even numbers are property identified
        Dim num As New Number
        num.Value = 5
        Assert.AreEqual(num.IsEven, False)
        num.Value = 8
        Assert.AreEqual(num.IsEven, True)
    End Sub

     _
    Public Sub TestIsOdd()
        ' Make sure odd numbers are property identified
        Dim num As New Number
        num.Value = 5
        Assert.AreEqual(num.IsOdd, True)
        num.Value = 8
        Assert.AreEqual(num.IsOdd, False)
    End Sub

     _
    Public Sub TestIsPositive()
        ' Make sure positive numbers are property identified
        Dim num As New Number
        num.Value = -1
        Assert.AreEqual(num.IsPositive, False)
        num.Value = 0
        Assert.AreEqual(num.IsPositive, False)
        num.Value = 1
        Assert.AreEqual(num.IsPositive, True)
    End Sub

End Class

You'll note that the test class is actually longer than the code that it's testing. This isn't unusual if you're doing a thorough job of test-driven development, because you'll usually want to test a variety of inputs for each part of your code. Figure 2 shows the MbUnit output for the classes in this state, with every test passing.

Successful test report from MbUnit



Page 2 of 3



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel