Gigi Labs

Please follow Gigi Labs for the latest articles.

Wednesday, October 30, 2013

C#: Unit Testing for Old or Express Visual Studio Editions Using NUnit

Hello folks! :)

I've written a few articles about unit testing so far. If you can use NUnit from within SharpDevelop, or use MSTest directly from within Visual Studio 2010+, then you are probably writing and running unit tests pretty comfortably.

Unfortunately, however, such luxury is not always available. Express editions of Visual Studio do not have the inbuilt MSTest features that the paid editions have; and older editions of Visual Studio did not have any unit testing functionality whatsoever. Fortunately, however, NUnit provides tools that can run unit test projects anyway. To show how this works, I'm going to demonstrate how to run unit tests for a project created in Visual Studio 2005.

The first thing we need is to download NUnit and then install it.

As we've already seen in "C#: Unit Testing with SharpDevelop and NUnit", this gives us the nunit.framework.dll file which we will need to reference in order to create our test project. You can find it in the bin\framework folder relative to wherever you installed NUnit:

Next, let's create our project via the File menu -> New -> Project... and then select Console Application and decide what to name your project and where to place it:

Make the Program class public so that our test project will be able to see it:

    public class Program

Then add the following method which we will be testing:

        public static String Shorten(String input)
            if (input.Length > 20)
                return input.Substring(0, 18) + "...";
                return input;

This method shortens a piece of text that is longer than a specified length (in this case 20 characters) - it truncates the string and adds "..." to show that there's more to it. This is sometimes useful when you need to fit text in a fixed width, such as on an internet forum.

Now, let's write some tests. The first thing we need to do is create our test project. And since we don't have the luxury of Unit Test Projects like we saw towards the end of "Unit Tests, The Visual Studio Way", we'll make a Class Library instead. A class library is a project that results in a .dll file that you can deploy with your executable - it's great for splitting up more complex projects. You can put your classes and other stuff in a .dll, and other projects can use them as long as they're public (that's why we made the Program class public).

Right click on the solution and go Add -> New Project... . Select "Class Library" as the project type, name it CsNUnitExternal.Tests, and hit OK.

Right click the new project, select "Add Reference..." (which might seemingly hang older versions of Visual Studio, but just be patient until it becomes responsive again), select the Browse tab, and locate the bin\framework\nunit.framework.dll file from your NUnit installation.

Add another reference, this time via the Projects tab, to the original project where your code is.

Delete the Class1.cs file that's created by default, and add a new class instead (right click on the new project, Add -> Class...), called ProgramTests. Near the top, add the following line so that we can use NUnit's functionality:

using NUnit.Framework;

Just beneath it, add the following, so that we can use the Program class and its Shorten method from our original project:

using CsNUnitExternal;

Add the TestFixture attribute on top of the class name so that NUnit can recognise it:

    class ProgramTests

Finally, add a couple of tests as follows:

    class ProgramTests
        void Shorten_LongString_Shortened()
            String input = "In the jungle the mighty jungle the lion sleeps tonight";
            String expected = "In the jungle the ...";
            String actual = Program.Shorten(input);
            Assert.AreEqual(expected, actual);

        void Shorten_ShortString_Shortened()
            String input = "Midnight Special";
            String expected = "Midnight Special";
            String actual = Program.Shorten(input);
            Assert.AreEqual(expected, actual);

This is what it looks like with colour and all - pretty enough to be placed inside a museum:

OK, now how do we run these tests? Conveniently, NUnit has this unit test runner program that we can use. You can get to it either from the bin folder in the NUnit installation, or via the Start menu (or whatever your Windows edition has instead).

Build your projects by pressing either F6 or Ctrl+Shift+B. Then, find your CsNUnitExternal.dll file in the relevant project's bin\Debug folder, and drag it onto the left part of the NUnit window (or load it via the File menu). The test class and relevant tests are loaded:

Running the tests now (Run button) will cause them to fail, and NUnit shows a big red bar as well as the reasons why the tests failed:

As the error messages indicate, we need to change our test class and methods to public, build the project again (at which point NUnit automatically reloads them), and then rerun the tests. We've gone green! :)

Sweet. In this article, we saw how we can use NUnit to run unit tests even when we have no in-built support for them from the IDE, such as old versions of Visual Studio (2008 and earlier), or Express editions of Visual Studio. We can do this by creating our tests in a class library, and then loading and running the tests in NUnit's own test runner, which is an external tool.

I hope you found this retro experience useful. Stay tuned for more articles coming soon! :)

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.