This project is read-only.
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)
        {
            Test<MyTestClass1>();
            Test<MyTestClass2>();
            Test<MyTestClass3>();
            Test<MyTestClass4>();

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

            Console.ReadLine();
        }

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

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

            HandleResults<T>(results);

        }

        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)
            {
                Console.WriteLine("---------------------------------");

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

            Console.WriteLine();
        }

    }
}

Last edited May 29, 2013 at 12:04 AM by obviouscode, version 17