views:

371

answers:

3

When doing Unit Testing, is there a way to tell the [TestClass()] to execute one [TestMethod()] by one? (Instead of Visual Studio to start multiple thread). This would be required for only one or two of my testing classes.

A: 

No, there is no way to do this in Visual Studio 2008 using the default tools.

It is possible by adding some ... interesting configuration code to the TestInit method. For instance, you could have all of your test classes derive from the following base class.

[TestClass]
public class ExecuteOneAtTimeBase {
  private static object s_mutex = new object();

  [TestInit]
  public void TestInit() {
    Monitor.Enter(s_mutex);
  }

  [TestCleanup]
  public void TestCleanup() {
    Monitor.Exit(s_mutex);
  }
}

All TestMethod instances are bracketed by calls to TestInit and TestCleanup methods. Using the Monitor.Enter/Exit combo you can guarantee that a given unit test method holds the lock for the duration of it's execution. Therefore multiple threads cannot be running different tests at the same time in a single AppDomain.

There are error cases where this could lead to a deadlock in the testing process. But I think that is probably a minor concern as it's not production code.

JaredPar
A: 

You can debug or run any single TestMethod.

  • Put your cursor in a TestMethod
  • to debug: Ctrl-R, Ctrl-T or Test -> Debug -> Tests In Current Context
  • to run: Ctrl-R, T or Test -> Run-> Tests In Current Context
p.campbell
This is really not what I want... If I have 2000 tests and only 100 needs to be run 1 by 1. I won't do it manually.
Daok
Sorry, I must have misread the question.
p.campbell
+1  A: 

If you are using Visual Studio 2008 Team Suite or (I believe) one of the Tester editions, you can create an Ordered Test and add all your unit test to that Ordered Test.

Mark Seemann