Implementing a Class Library in VB.NET
In this tutorial we will look at the steps needed to test and develop a VB.NET class library using csUnit.
This tutorial uses Visual Basic.NET.
This tutorial assumes that you have installed:
Steps to be taken
Here is a list to give you a quick overview on what we are going to do:
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.
Now we will set up the project which we are going to test. The Steps are the same as 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.
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.
Imports csUnit <TestFixture()> _ Public Class BasicArithmeticsTests End Class
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
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
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.
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:
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
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).
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.