Unit Testing                                                                                        
•        Unit testing is the process of testing a section (a unit) of code to make sure it performs correctly using
varied data.
-        Typically, this refers to testing methods and properties, which can be tied to events, fields and even
other methods.
-        Unit testing is often performed by calling methods, passing in different pieces of data and analyzing the
results.
-        Two other types of unit test include data-driven unit tests (based on rows in a data source) and ASP.
NET unit tests.
-        If the results are within the developer’s expectations, then the code can considered “unit tested” and can
be checked in to a SCM.
-        Unit testing often involves the use of “happy paths” and “sad paths”.
        Happy paths are when the expected data is passed in with expected results.
        Sad paths are when erroneous, unexpected pieces of data are passed in, hopefully with expected
results.
•        Before Visual Studio 2005, Unit testing required the developer to create custom test code or use nUnit.
-        Custom code (often referred to as a test harness) was created to call the methods and pass in variable
pieces of data.
-        nUnit is free and can be used with several other tools to make life easier within the VS IDE, such as
TestDriven.NET and TestComplete. It can be downloaded from here: http://www.nunit.org/

•        Test Driven Development (TDD) is the process of creating the tests before writing the code.
-        TDD was originally based as part of Extreme Programming (XP) with Agile.
-        When tests are created first, the code can be more accurately constructed on the first attempt - meeting
the developer’s expectations. In effect, less time is spent on writing code.
-        Visual Studio 2005 supports TDD by allowing the developer to add empty methods or properties to
classes or structures. By default, all methods and properties should fail when executed. Once populated with
code, they should succeed.
-        The tests can be created to test these empty methods. They will not be run until after the methods of
been populated. It’s been said that nUnit may have much better support for TDD.
-        You can learn more about TDD from the MSPress book titled, “Test-Driven Development in
Microsoft .NET”, ISBN: 0735619484
•        Visual Studio 2005 can create a Unit Test for you based on an existing project, namespace, class or class
member.
-        When you create your first unit test, it is added to a C#, VB or C++ unit test project, which is added to
your current solution.
-        With this C# or VB test project, you can choose what sorts of data should be passed into you methods
or set in your properties.
-        Admittedly, it is a bit time consuming to fill in these methods, but it is better than creating the entire
test framework from scratch.
-        Remember – unit testing is supported with Team Developer, Team Tester and Team Suite.

•        A Simple Walkthrough: Creating a unit test for a simple Math Library.
-        We are creating a simple VB Math Class Library that performs the four classic functions based entirely
on Integers.
-        It has one class called Compute, which holds the four methods.
Public Class Compute
Public Function Add(ByVal x As Integer, ByVal y As Integer) As Integer
Return x + y
End Function

Public Function Subtract(ByVal x As Integer, ByVal y As Integer) As Integer
Return x - y
End Function

Public Function Multiply(ByVal x As Integer, ByVal y As Integer) As Integer
Return x * y
End Function

Public Function Divide(ByVal x As Integer, ByVal y As Integer) As Integer
If y <> 0 Then
Return x \ y
End If
Return 0
End Function
End Class
•        There are several different ways to create Unit tests including:
-        Right-click the class in the source view and choose “Create Unit Tests…”
-        Right-click one of the methods and choose “Create Unit Tests…”
-        Right-click the solution in Solution Explorer and choose “Add – New Project…”
        On the left side, choose the language of your choice and click on “Test”.
        Give your Test Project a name and click “OK”.
-        Click the dropdown menu “Test – Windows – Test View” and click on “Create New Test”. We’ll
discuss the Test windows in the next chapter.
•        Let’s create a test project for our Compute class by right-clicking Compute and choosing “Create Unit
Tests…”

•        Immediately, we are prompted to choose which class members to test and the language we would like
the test project to use. We’ll test all four methods and stick to VB, giving it the name “TestMathLib”.


•        In the Solution Explorer window, we can the newly created VB test project. Also notice the two new
files added at the solution level.

-        The *.testrunconfig file is an XML file holding configuration settings such as scripts that should be run,
test timeouts, Code Coverage, deployment settings, etc.
-        AuthoringTests.txt gives useful information about the test project.
-        ComputeTest.vb (named accordingly, in this example) is a class file that was generated to perform
testing against the Compute class.
•        As you can see on the following page, one of the test methods from the ComputeTest class is displayed.
-        Notice the naming pattern for the AddTest method.
-        Check out the TODO’s that must be completed by the developer for accurate testing, along with
variables titled “Actual” and “Expected”.
-        Finally, notice the inclusive “Assert” at the end of the method.


'''<summary>
'''A test for Add(ByVal Integer, ByVal Integer)
'''</summary>
<TestMethod()> _
Public Sub AddTest()
Dim target As Compute = New Compute

Dim x As Integer 'TODO: Initialize to an appropriate value

Dim y As Integer 'TODO: Initialize to an appropriate value

Dim expected As Integer
Dim actual As Integer

actual = target.Add(x, y)

Assert.AreEqual(expected, actual, _
"MathLib.Compute.Add did not return the expected value.")
Assert.Inconclusive("Verify the correctness of this test method.")
End Sub
•        Let’s modify the Add method to perform a given test, like this:
Dim x As Integer = 100 'TODO: Initialize to an appropriate value
Dim y As Integer = 100 'TODO: Initialize to an appropriate value

Dim expected As Integer = 200
Dim actual As Integer = 0
•        To run our unit test, we will click on “Test – Start Selected Test project Without Debugger”.
-        As you can see, a Test Results window pops up while the tests run. The results state the tests as
inconclusive.
-        

•        However, if we comment out the call to Assert.Inconclusive(), it passes!


•        If you think about it, you can add all sorts of code to your unit tests.
-        You could modify the methods to use “foreach” loops, working with arrays or collections of data.
-        You could make the methods call other methods simultaneously to test shared data.
-        With Visual Studio 2005, you can set breakpoints in your test methods and analyze results as the test
progresses.
Unit Testing
Table of Contents
Copyright (c) 2008.  Intertech, Inc. All Rights Reserved.  This information is to be used exclusively as an
online learning aid.  Any attempts to copy, reproduce, or use for training is strictly prohibited.
Courseware
Training Resources
Tutorials