Unit Testing in Visual Studio Express 2008

By torgeirhelgevold
In an earlier blog post I described how to use Nunit for unit testing in Visual Studio Express. One short coming of this strategy is that you can’t easily debug your tests. This is because Visual Studio Express doesn’t seem to let you attach external processes. (We would need to attach the external Nunit process to step into the code).

To get around this rather annoying problem, I have created my own testing framework; ExpressUnit. The current version of ExpressUnit is now 3.x. The latest version offers two new benefits over previous versions:

  1. The new UI interface has been created using WPF
  2. ExpressUnit 3.x supports continuous integration with Hudson which means you can use ExpressUnit to run your  tests on your build server. In my current development environment I have configured ExpressUnit to run all tests on every code check-in. My continuous Integration environment is currently set up using Subversion, Hudson and ExpressUnit.

Instead of relying on an external application to run the tests, ExpressUnit will run from within your Visual Studio Solution. The idea is that you download ExpressUnit in the form of a Visual Studio project and add it to your solution.

The added project becomes your unit test project where you may add your test classes with the belonging unit tests. No configuration is required.

To add tests, simply add a test class with public test methods. The class and methods must be decorated with the [TestClass] and [UnitTest]/[ItegrationTest ] attributes respectively. The unit test attribute should be used for unit tests and the Integration test attribute is meant for integration tests. ExpressUnit will let you filter test runs on these attributes.

It’s usually my preference to add all my tests to the ExpressUnit project, but ExpressUnit allows for tests to be defined in any project in your solution (as long as the ExpressUnit project has a reference to it).

Anyway, that’s enough background information! To make this as useful as possible I will demonstrate how to use ExpressUnit through an example.

Step1:

Download ExpressUnitGui from

http://code.google.com/p/expressunit/

Step2:

Add the ExpressUnitGui project to your already existing Visual Studio 2008 solution.

After completing this step, your solution will look similar to Figure1

Figure1

Figure1

Step3:

Start writing your first ExpressUnit test.

As mentioned earlier, you must start by adding a test class with at least one test method to the ExpressUnitGui project. The class may look something like the following:

[TestClass]

public class SampleTest

{

[UnitTest]

public void AddTest()

{

MathClass math = new MathClass();

Confirm.Equals(4,math.Add(1,3));

}

}

Step4:

Run your test!

To run your tests, select ExpressUnitGui as your startup project and press F5

The UI window will load with a simple interface to execute one or more of your tests. (Figure2). You may run entire test suits or individual test by right clicking the appropriate tree node in the tree view, but to run all tests at once, all you have to do is click the green “Play” button. The test results are shown in the right pane, and as you can see in Figure2, each test result offers an expand button to view more details about failing tests.

There is also an option to run all tests automatically on start-up of ExpressUnit. This feature is controlled by the runTestsOnStartup flag in the application’s App.Config (Default value is false)

Figure2

Figure2

API

The following is an overview of the API offered by ExpressUnit

Confirm methods

The confirm class contains several static methods used to confirm the outcome of your tests

Confirm.Equals(object obj1, object obj2):

This method compares two objects for equality by using the object’s own Equals implementation.

Confirm.Equals will throw EqualsException if the two objects are not equal.

Confirm.Different(object obj1, object obj2):

This method is the inverse of Confirm.Equals, and is used to check if two objects are unequal.

UnEqualityException is thrown if the two objects are equal.

Confirm.IsGreater(long/double val1, long/double val2)

This method is used to test if val1 is numerical bigger than val2

Confirm.SameCollections(IEnumerable param1, IEnumerable param2)

This method will test if two objects of type IEnumerable are the “same”, and the definition of “same” is that all contained objects are equal.(based on the contained object’s own Equal implementation. For example, it may be used to compare the content of two arrays or collections.

EqualityException is thrown if the two instances are not the “same”.

Confirm.ExceptionThrown(Type expectedExceptionType,TargetMethod target)

This method will check if the target method throws the expected exception. The target method is passed in as a delegate of type TargetMethod.

Attributes

The following are attributes defined by ExpressUnit

[TestClass]

This attribute defines a class as a test class.

[UnitTest]

This attribute defines a method as a unit test

[IntegrationTest]

This attribute defines a method as an integration test

[Ignore]

This attribute will cause a test to be ignored when running your tests

[ExceptionThrown(Type exceptionType)]

This attribute can be put on a test to indicate that an exception of type exceptionType is expected to be thrown. The test will fail if the exception is not thrown.

Conclusion

If this this article has made you curious about ExpressUnit, you may download it from

http://code.google.com/p/expressunit/

Don’t hesitate to drop me a line if you have comments or would like to see additional functionality in ExpressUnit.

Tags: , , , , , , , , , ,

13 Responses to “Unit Testing in Visual Studio Express 2008”

  1. torgeirhelgevold Says:

    Updated:

    I have put out a new version of ExpressUnit:
    More info can be found here:
    http://torgeirhelgevold.wordpress.com/wp-admin/post.php?action=edit&post=15

  2. Pete Says:

    Thanks! Fantastic tool. It would be even better if I could put my tests in my own project, and just use the ExpressUnit project to run them… I am new to C# so do let me know if there’s an obvious way.

  3. torgeirhelgevold Says:

    Hi Pete.
    Based on your comment, I have added some more flexibility to ExpressUnit. (version 1.6)
    You will now be able to define tests in any project in your Visual Studio solution.

    All you have to do is add a reference from your project to the new dll; expressUnitModel.dll. (I have moved the test attributes to this assembly)

    Now, when you run the ExpressUnit project, it will automatically detect tests from any project it has a reference to.

    Tor

  4. Pete Says:

    Fantastic! Thanks.

    And just one more thing that I think would finish it off in making it a really useful tool… If my Confirm.Equals fails, it would be very useful if the GUI would perform a ToString on the two objects provided to it, so it’s obvious why the test failed. This is easy in my native language of Java, I assume it is in C# too….

    Good work :)

  5. torgeirhelgevold Says:

    No problem.
    Thanks for showing interest in the project.

    I have put out a new version with a more meaningful message when Confirm.Equals / Confirm.Different fails

    Tor

  6. torgeirhelgevold Says:

    I have put out a new improved version of ExpressUnit
    Download from http://code.google.com/p/expressunit/

  7. Sagi Says:

    I have just downloaded the project from google code but I don’t know how to install it. I guess I need to grab the ExpressUnitModel.dll from the bin/Release and add a reference to it in my project but I’m not sure regarding the Gui.exe and whether anything else is needed.
    Thanks,

    • torgeirhelgevold Says:

      The easiest way is to just add the ExpressUnitGui project to your solution. (Described as step 1-4 in the article above)
      That way you can add all test to the added project

      • Sagi Says:

        Thanks, I followed the instructions and added a class to the ExpressUnitGui project:

        using System;
        using System.Collections.Generic;
        using System.Linq;
        using System.Text;

        namespace ExpressUnitGui
        {
        [TestClass]
        public class TimerTest
        {
        [UnitTest]
        public void AddTest()
        {
        Confirm.Equals(4, 2 + 2);
        }

        }
        }

        I’m getting errors about the TestClass and UnitTest attributes not being found.
        Any idea?
        Thanks in advance and sorry if I’m missing something very basic here, I’m a newbie with VC#

    • torgeirhelgevold Says:

      Hi Sagi.
      It looks like you are just missing a using statement.
      Just add the following to the top of the class:

      using ExpressUnitModel;

      This will tell the compiler where the attributes are defined

  8. Sagi Says:

    Cool. That did the trick. Thanks!

    One more question – to get the test to run once the ExpressUnit GUI comes up I need to right-click on the specific test in the left pane and select RUN. Is there a way to get ExpressUnit to run the tests immediately when it comes up?

    • torgeirhelgevold Says:

      I agree. That is a useful feature.
      I have uploaded a new version (ver 2.2) where this is supported.

      Just set the RunTestsAtStartup config setting in app.config in the ExpressUnitGui project to True (The value is case sensitive), and all tests will be run on startup.

      The default value is False

  9. torgeirhelgevold Says:

    Soon I will post a new blog entry describing how to set up continuous Integration with ExpressUnit and Hudson

Leave a Reply