Baccou Bonneville Blogs Eclipse Blog Process Improvement Blog Java Blog Web Design Blog Miscellaneous .NET Blog

01/10/06

English (US)   Unit Testing in .NET  -  Categories: .NET, Visual Studio, Tools, NUnit, Unit testing, VSTS  -  @ 04:52:11 pm

Traffic LightThis 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.

[More:]

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:

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:

NUnit GUI

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:

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:

VSTS Unit Test Results

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.

VSTS Code Coverage

See also

Nunit V.2 for People Who Can't Cook

2 comments

Comments:

Comment from: Andrei Csibi [Visitor] Email · http://andrei.csibi.ro
The link for Executing VSTS Unit Tests and Code Coverage from the Command Line points to a strange page without any explanations.
PermalinkPermalink 06/01/07 @ 23:22
Comment from: admin [Member] Email · www.baccoubonneville.com
Thank you. It's corrected.
PermalinkPermalink 06/02/07 @ 07:45

Leave a comment:

Your email address will not be displayed on this site.
Your URL will be displayed.

Allowed XHTML tags: <p, ul, ol, li, dl, dt, dd, address, blockquote, ins, del, span, bdo, br, em, strong, dfn, code, samp, kdb, var, cite, abbr, acronym, q, sub, sup, tt, i, b, big, small>
(Line breaks become <br />)
(Set cookies for name, email and url)
(Allow users to contact you through a message form (your email will NOT be displayed.))
This is a captcha-picture. It is used to prevent mass-access by robots.

Please enter the characters from the image above. (case insensitive)

Pingbacks:

No Pingbacks for this post yet...

powered by
b2evolution

Credits: blog software | web hosting | monetize