01/10/06
Unit Testing in .NET -
Categories: .NET, Visual Studio, Tools, NUnit, Unit testing, VSTS -
admin
@ 04:52:11 pm
This blog note will deal with unit testing in .NET using Visual Studio 2005. It will present two options to implement unit tests in .NET: NUnit and VSTS.
1. Test-driven development (TDD)
First, I want to start this blog note by the definition of test-driven development (TDD). TDD is one of the principles promoted by Extreme Programming. As explained in test driven development definition by Wikipedia, TDD aims to develop the test before writing the code. This note will see how one can implement TDD in .NET (using C#).
2. NUnit or VSTS?
To perform unit testing in .NET, you can use:
- NUnit: it is an open source software. It's a port of JUnit for .NET.
- Visual Studio Team System (VSTS): it is a particular edition of Visual Studio that includes unit tests. More information about VSTS can be found by reading this article: Write Maintainable Unit Tests That Will Save You Time And Tears.
We have a deeper look on NUnit with a sample.
3. NUnit sample
First, you should install NUnit. You should also install TestDriven.NET, an NUnit add-in for Visual Studio.
In our sample, we will have a console program, a library and a test library. The console program is using the library. The test library is tested the library.
Create a new solution called "NUnitSample" with a console application called "Application". Then, create a new "Class Library" project called "Library" and another "Class Library" project called "LibraryTest".
In the project called "Library", rename the file into "User.cs". This is the code of this file:
using System;
using System.Collections.Generic;
using System.Text;
namespace Library
{
public class User
{
public String Firstname;
public String Lastname;
public String GetFullname()
{
return Firstname + " " + Lastname;
}
}
}
Compile the library using Shift + F6.
In the "Application" project, this is the code of Program.cs:
using System;
using System.Collections.Generic;
using System.Text;
using Library;
namespace Application
{
class Program
{
static void Main(string[] args)
{
User u = new User();
u.Firstname = "John";
u.Lastname = "Woo";
Console.WriteLine(u.GetFullname());
}
}
}
Add a reference to the Library dll using right-click on the project > Add Reference > Projects.
Run the application by using Ctrl + F5. You should see "John Woo" on a console window.
Now, it's time to write the test class. In "LibraryTest" project, rename the .cs file into "UserTest.cs". This is the code of this file:
using System;
using System.Collections.Generic;
using System.Text;
using NUnit.Framework;
using Library;
namespace LibraryTest
{
[TestFixture]
public class UserTest
{
private User _user;
[SetUp] public void Init()
{
_user = new User();
}
[Test] public void GetFullname()
{
_user.Firstname = "John";
_user.Lastname = "Woo";
Assert.AreEqual("John Woo", _user.GetFullname());
}
}
}
You must add a reference to the DLL to test. You must also add a reference to the NUnit framework dll (c:\Program Files\NUnit-Net-2.0 2.2.5\bin\nunit.framework.dll).
Build the test library using Shift + F6.
Run the tests by using right-click on the project > Run Test(s). You should have "1 Passed, 0 Failed, 0 Skipped" on the bottom of your Visual Studio window.
You can also launch the NUnit tests using NUnit GUI: Right click on the test project > Test With > NUnit, then click on "Run". The following screen is displayed:

4. VSTS Unit Testing Framework
If you want to use the Unit Testing Framework from Visual Studio 2005, you need Visual Studio Team System. Everything is clearly explained in these articles:
- Unit Testing and Generating Source Code for Unit Test Frameworks Using Visual Studio 2005 Team System
- A Unit Testing Walkthrough with Visual Studio Team Test
With VSTS, you will have a new menu "Create Unit Tests" when you right click on your code.
This is a screenshot of the test results screen in VSTS:

To run your tests in batch mode (without Visual Studio), you should follow instructions from this article: Executing VSTS Unit Tests and Code Coverage from the Command Line.
To summarize, you will have to open a Visual Studio 2005 Command Line, go in your project and type:
mstest /testcontainer:LibraryTest\bin\Debug\LibraryTest.dll
To see how much code is covered by your unit tests, you should run your test in without debugger (Test > Start Selected Test Project without Debugger). You will have a Code Coverage Results tab. This will tell you the percentage of the code covered by your tests. In your code, you will see in blue background the tested code and in red background the uncovered code.

See also
Nunit V.2 for People Who Can't Cook
Comments:
Leave a comment:
Pingbacks:
No Pingbacks for this post yet...