Creating a Test

Using the Test Project template, add a new project named Foo.Math.Tests to the solution, as shown in Figure 7-4. This will add a single class, UnitTest1.vb, to your solution (renamed to AverageTests.vb in Figure 7-5). Delete that class for now, as you will add your own in a moment. In addition, three solution items are added; these enable configuring how the testing will run. You will look at these later. For now, add a reference to the Foo.Math project.

Add Mew Project

mrai

Recent Templates

[.NETFramework4 » Sort by: Name Ascending

- [': 111! lH)| Search Installed Terr P \

Installed Templates

J Visuul Dflsic

T«t Prnjirt ViciHl Ratlr

Type: Visual Basic A project that contains tests.

Windows

Web

t orrite

Cloud Service

Reporting

Share^oint

Sihreiliuht

Test

WCF

WorttTlow

Visual O

i Visual F*

Other Project Types

Database

Mudeliny Projects

1 Test Projects

Online Templates

Name: l-oo.Math Jests

Location; C\worfc\wiili»g\boofcs\4Jiu»b2010,'C7-TDD\i.uUeWuik\5ldlsTeU «• |

Browse,.. |

Oi | ranrn |

figure 7-4

Right-click on the test project. Select Add O New Test. In the Add New Test dialog (see Figure 7-6), select Basic Unit Test and call the new file AverageTests.vb. Click OK to add the new test to your project. The initial code for the unit test provides the core structure needed by all tests:

Imports System.Text

<TestClass()> Public Class AverageTests

<TestMethod()> Public Sub TestMethod1() End Sub

End Class

As you can see, the test class is simply a normal Visual Basic class, with the addition of a couple of attributes.

The TestClass attribute identifies this class to the testing functionality of Visual Studio; it has no other purpose beyond marking this as a class containing one or more tests.

The TestMethod attribute identifies one of those tests within the class. Like the TestClass attribute, this is primarily a marker attribute. Each of the tests will use this attribute to identify itself. This allows you to have other helper methods within the class that are not tests. Notice also that each of the tests is a Sub, not a Function.

You're ready to begin to create some test methods. As with many other discussions regarding TDD, there are a number of opinions about the scope of each test. Some developers choose to test a number of separate cases within a single method, while others prefer to keep the scope of each test relatively simple. This book falls

5oluti(]ii hckrer

.il-ii^tmii

Solution StatsTcst' (2 protects) Solution Items

LocaUestsettlngs _ itatslert.wmdr

TrateAndTcsUmpacttcstsethnqi Foii.Miilli 1=31 My Project stats,vb Foo,Math,Tests

-J] My Pinjcit

AverageTeits.vO

figure 7-5

figure 7-6

into the second camp. Keeping your tests simple offers the same benefit as keeping any other method simple. That is, when your tests are simple and have a single purpose, there is less room for introducing errors.

Your test methods should be named such that you don't need to look into their code to understand what they are testing. For example, a test named TestForDivideByZero is a lot more meaningful than a test method called Testl. Again, this saves you time later should any of your tests fail.

For the simple Average method defined above, add the following tests (you could no doubt come up with more):

Imports System.Text

<TestClass()> Public Class AverageTests Dim obj As New Foo.Math.Stats

<TestMethod()> Public Sub AverageOfKnownRange()

Assert.AreEqual(obj.Average(1, 2, 3, 4, 5, 6, 7, 8, 9, 10), 5.5, "Average of 1-10 is not 5.5")

End Sub

<TestMethod()> Public Sub AverageOfZerosIsZero() Assert.AreEqual(obj.Average(0, 0), 0.0,

"Average of zeros is not zero")

End Sub

<TestMethod()> Public Sub AverageOfNothingIsNaN()

Assert.AreEqual(obj.Average(), Double.NaN) End Sub

<TestMethod()> Public Sub AverageOfMaxValuesIsInfinity()

Assert.AreEqual(obj.Average(Double.MaxValue, Double.MaxValue), Double.Positivelnfinity)

End Sub End Class

Code snippet from StatsTest

Here, four tests are defined; each tests for different combinations of good and bad parameters. The first test is an attempt to see if the function is working as designed, providing a set of values and checking the result. The Assert.AreEqual method used here takes three parameters:

V The method to test (or rather the result of that method)

V The expected result value

V A message to display in the user interface if an error occurs

The second test begins to examine some possible edge case scenarios — in this case, whether all zeros are passed to the Average method. Notice that the value to compare with the result has been written as 0.0, not just 0. This is because it would fail if it were written as 0, as you would be attempting to compare a Double with an Integer.

The final two tests check for two other edge cases: What happens if no values are passed in, and what happens if you use values that add up to greater than the capacity of the Double variable you are using to hold the temporary sum. Both simply check the result against a known value (Double.NaN, or Not a Number, and Double.Positivelnfinity).

Project Management Made Easy

Project Management Made Easy

What you need to know about… Project Management Made Easy! Project management consists of more than just a large building project and can encompass small projects as well. No matter what the size of your project, you need to have some sort of project management. How you manage your project has everything to do with its outcome.

Get My Free Ebook


Post a comment