Standalone Unit Test Programs

Published 29 December 06 10:20 PM | admin 

In the project that I'm working on we wanted to have certain of our MSTest unit tests be available as standalone programs.  Various reasons for this include:

  • It is useful for demos
  • It's makes certain debugging scenarios a little easier, i.e. those where multiple VS instances are required.
  • It avoids having to remember the mstest command line syntax.

In particular we didn't want to give up the unit test UI inside VS, and we didn't want a slew of little test .exe's with secret command line syntax required to run them.

For the time being, the solution that I came up with is to invoke the unit test assembly containing the actual tests from a very simple .exe wrapper, like this:

using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;

namespace MyProject.UnitTest.TestWrapper
{
    class Program
    {
        static int Main()
        {
            ConsoleTraceListener consoleListener = new ConsoleTraceListener();             
            
            consoleListener.Name = "TestWrapper";
            Debug.Listeners.Add(consoleListener);
            
            try
            {
                MyClassTests tests = new MyClassTests();
                
                tests.TestSomething();
                
                return 0;
            }
            catch (Exception e)
            {
                Debug.WriteLine(e);
                return -1;
            }
        }
    }
}

The test itself can just write messages to the Debug or Trace classes.  That way you can still run the test in the IDE and see the trace messages in the Output tool window.

Comments

# The Director of Random Technologies said on July 23, 2007 4:01 PM:

While I haven't been posting much here, I have been posting pretty regularly over on my main development

# Noticias externas said on July 23, 2007 5:01 PM:

While I haven't been posting much here, I have been posting pretty regularly over on my main development

Anonymous comments are disabled