Implementing a Class Library in VB.NET

by Markus Renschler

Introduction

In this tutorial we will look at the steps needed to test and develop a VB.NET class library using csUnit.

Programming Language

This tutorial uses Visual Basic.NET.

Prerequisites

This tutorial assumes that you have installed:

  • Visual Studio .NET (here called: VS.NET)
  • csUnit 1.7.4 or later

Steps to be taken

Here is a list to give you a quick overview on what we are going to do:

Create the solution

  • Open VS.NET
  • Select "File --> New --> Blank Solution" from the Pull down menu.
    Then you will see the "New Project" Window
  • Enter "MyCalculator" as the solution name
  • Specify a folder of your choice to store the solution in
  • Click "OK" and the empty solution will be created

Create the test project

We have an empty solution. Now our first step will be to set up the project which contains the tests. This project will be a class library because csUnit does not need a special entry point.

It will find test methods through the attributes we give them.

  • Select "File --> Add Project --> New Project --> Visual Basic Projects --> Class Library"
  • Enter "MyCalculatorTests" as the name
  • Click "OK"
  • Unfortunately VS.NET always creates its default class (called "Class1") when you add a project. It has shown to be the fastest way to delete this class and to add the own ones later. So we delete "Class1" from the project (you can do this by right-clicking on the class name in the Solution Explorer and selecting "Delete").
  • The solution contains one empty project now and should look like this:

Create the class library project

Now we will set up the project which we are going to test. The Steps are the same as for the test project:

  • Select "File --> Add Project --> New Project --> Visual Basic Projects --> Class Library"
  • Enter "MyCalculator" as the name
  • Click "OK"
  • Remove "Class1" from the project

Set up references for the test project

In order to let the test project know where it can find the classes that it shall use (csUnit classes and the classes from the class library to be tested) we need to set up references to these Projects/Assemblies.

  • In the Solution Explorer Right-Click the "References" node of the project "MyCalculatorTests"
  • Select "Add Reference ..."
  • In the .NET tab select the item with the component name "csUnit" by double clicking or by marking it with a left mouse click and pressing the "Select" button:
  • In the Projects tab select "MyCalculator" as reference to be added:
  • Now click "OK"
  • The tree for the test project should look like this in the solution explorer now:

Create a Test Fixture

Now let's go to the real task. In this step we will set up a test fixture. A test fixture is a class which contains test methods which will be executed by csUnit.

  • First we create a new class in the test project:
    Select "File --> Add New Item --> Class"
  • Enter "BasicArithmeticsTests.vb" as the name
  • Click "Open"
  • Now you should see the new class in the code window.
To define this class as a test fixture we need to assign the corresponding csUnit attribute now. Modify the class code like this:
Imports csUnit
<TestFixture()> _
Public Class BasicArithmeticsTests

End Class
The imports statement tells VB.NET to import the csUnit namespace which contains the TestFixture Attribute.

Add a Test

Now the big moment has come. We are going to create the first test. Our first test consists of the following code:

Imports csUnit
Imports MyCalculator

<TestFixture()> _
Public Class BasicArithmeticsTests
    <Test()> _
    Public Sub Add()
        Dim ba As BasicArithmetics
        ba = New BasicArithmetics()
        Assert.Equals(3, ba.Add(1, 2))
    End Sub
End Class
Note: The <:Test()> attribute tells csUnit that the Sub "Add" shall be executed by csUnit as a test.

As soon as you try to build this you will see that the compiler does not like it. There isn't a BasicArithmetics class.

Let us do something against that: Create a new class called "BasicArithmetics" in the project "MyCalculator". Initially this class shall have the following code:

Public Class BasicArithmetics
    Public Function Add(ByVal num1 As Integer, _
                ByVal num2 As Integer) As Integer
        Add = 0
    End Function
End Class
Yes, you are right. This Add method will always return a 0.

We strongly recommend you to write tests in such an order that you have seen each test as failed (red) before you add the functionality to make the test successful (green). That is the only way to make sure that the program does exactly what the tests demand.

Now the first test and the first part of the program are written. The Solution should build now. Then we can go on and run the test.

Run the Test --> bar red

Before we run the tests please make sure that the test project is defined as StartUp project. This is the case if the project name of the test project "MyCalculatorTests" is written in bold letters in the solution explorer. If this is not the case right-click on the project name and select "Set as StartUp Project".

This is necessary for csUnit to know in which project it shall look for tests. Running the tests is simple:

  • From the Tools menu select "csUnit for Visual Studio".

    Then csUnit should start up.
  • Click on "Run" and (hopefully) you will see this:
Now we have run the test and csUnit indicates that there must be some functionality missing in our program.

Write class library code

In the previous step csUnit reported us that the expected result had been 3 but actually the program returned 0.

Fine, that is the expected behaviour. Now we can add functionality to the Add method:

Public Class BasicArithmetics
    Public Function Add(ByVal num1 As Integer, _
                ByVal num2 As Integer) As Integer
        Add = num1 + num2
    End Function
End Class

Run the Test again --> bar green

Now we build the program again and then we start csUnit (If it is still open you can use the open instance. csUnit reloads all classes on each test run so that changes are considered).

The result looks like this:

The green bar indicates that no test reported a problem and that the program works perfectly (at least in the areas which are being covered by tests; hopefully there are no different areas).

Summary

In this tutorial I tried to show how to build a VB.NET class library test driven and from scratch. Intentionally I left out many csUnit features just to show how the project is being set up.

If you have set up the sample solution like I explained it here you are able to play around.

Just have a look into the other csUnit tutorials. Most of the samples are written in C# but now you have a VB.NET "base plate" and it should be relatively easy to translate the C# examples into VB.NET.

Happy Testing!

Related Topics


Copyright © 2002-2007 by Manfred Lange. All rights reserved. Last change: 3/20/2007. Site design by Andreas Weiss.
This site is protected by bot traps.

Home

Download

Documentation

Features

Tutorials

Support

Suggest A Feature

Report A Bug

About



Web csunit.org

Sponsors:

Extreme Simplicity Logo

Agile Utilities Logo

Blue Note Ventures Logo


Sources hosted by
Get csUnit - unit testing for .NET at SourceForge.net. Fast, secure and Free Open Source software downloads



Copyright © 2002-2008 by Agile Utilities NZ Ltd. All rights reserved. Site design by Andreas Weiss. This site is protected by bot traps.