Use NHarness to debug Unit Tests in Visual Studio Express

NHarness was primarily written to allow Visual Studio Express users, without access to plugins such as TestDriven.NET, to run their tests in debug mode, in the Visual Studio IDE.

A simple RunTestsInClass<T> static method is called, and a detailed TestResult enumeration is returned.

The downloadable dll was compiled using NUnit. To use other frameworks, download the sourcecode and simply replace the framework reference. I have already done this for MbUnit (this required a small change to the ExpectedException handling, to point to the ExpectedExceptionType). This library is also available in downloads.

Tests can be run using the Tester.RunTestsInClass<T>. The class T should be decorated with the TestFixtureAttribute, and will recognise the following attributes:
  • TestFixtureAttribute
  • TestFixtureSetUpAttribute
  • TestFixtureTearDownAttribute
  • SetUpAttribute
  • TearDownAttribute
  • TestAttribute
  • ExpectedExceptionAttribute (as well as the ExpectedException type and message parameters)

Console example of using NHarness TestRunner

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

namespace Harness
    class MainClass
        static int _tests;
        static int _passed;
        static int _failed;

        public static void Main(string[] args)

            Console.WriteLine("Ran {0} Tests. {1} Passed, {2} Failed.", _tests, _passed, _failed);


        static void Test<T>()
            TestResults results = Tester.RunTestsInClass<T>();

            _tests += results.NumberOfResults;
            _passed += results.NumberOfPasses;
            _failed += results.NumberOfFails;



        static void HandleResults<T>(TestResults results)
            if (results.NumberOfFails == 0)
                Console.WriteLine("{0}: PASSED", typeof(T));
            DocumentFails(ref results);

        static void DocumentFails(ref TestResults results)
            if (results.NumberOfFails > 0)

                foreach (TestResult result in results)
                    if (result.Result == TestResult.Outcome.Pass)
                    Console.WriteLine("Test {0}: {1} {2}", result.MethodName, result.Result, result.Result == TestResult.Outcome.Fail ? "\r\n" + result.Message : "");



Last edited May 28, 2013 at 11:04 PM by obviouscode, version 17