1

I googled for this issue but could not find the answer.

My test runs appear to run in parallel and cause each other to fail. They do all pass when run individually. I tried to add thread in the test and put them to sleep but no luck.

Is there a way to run these tests in sequence one after another?

My environment:

Visual Studio 2010 Resharper Jet brains 6.1

dotnet-practitioner
  • 13,968
  • 36
  • 127
  • 200
  • Sounds to me that you have static data somewhere that is persisting from one test to another that may be the underlying cause of your issue. As Peter Ritchie suggests you should rewrite the code/tests so this static dependency doesn't affect other tests. – JG in SD Dec 21 '12 at 18:13
  • 1
    I made this mistake in some of my first tests too. I would set a value in the DB in Test1 that Test2 would then expect to be there. It was a recipe for disaster. – Nick DeVore Dec 21 '12 at 18:16
  • possible duplicate of [Is there a way to \*prevent\* ReSharper from running an assembly's unit tests in parallel?](http://stackoverflow.com/questions/10017156/is-there-a-way-to-prevent-resharper-from-running-an-assemblys-unit-tests-in-p) – t0mm13b Dec 21 '12 at 20:14

1 Answers1

11

I would suggest you have unit tests that are deterministic. That is they don't depend on the order they are run or that other tests be run before or after. Not doing this is a recipe for failure. Most test runners are based on the fact that test methods are completely independent.

This fact is inherently obvious in the way the methods of a test class are invoked. e.g. with MS Test you can have Assembly, Class and Test initialize methods. All of these are invoked for each TestMethod being invoked. For example, with the following class:

   [TestClass()]
   public class DivideClassTest
   {
      [AssemblyInitialize()]
      public static void AssemblyInit(TestContext context)
      {
         Console.WriteLine("Assembly Init");
         }

      [ClassInitialize()]
      public static void ClassInit(TestContext context)
      {
         Console.WriteLine("ClassInit");
      }

      [TestInitialize()]
      public void Initialize()
      {
         Console.WriteLine("TestMethodInit");
      }

      [TestCleanup()]
      public void Cleanup()
      {
         Console.WriteLine("TestMethodCleanup");
      }

      [ClassCleanup()]
      public static void ClassCleanup()
      {
         Console.WriteLine("ClassCleanup");
      }

      [AssemblyCleanup()]
      public static void AssemblyCleanup()
      {
         Console.WriteLine("AssemblyCleanup");
      }

      [TestMethod()]
      public void Test1()
      {
          Console.WriteLine("Test1");
      }
      [TestMethod()]
      public void Test2()
      {
          Console.WriteLine("Test2");
      }
  } 

You'll see output like

Assembly Init
ClassInit
TestMethodInit
Test1
TestMethodCleanup
TestMethodInit
Test2
TestMethodCleanup
ClassCleanup
AssemblyCleanup

Although there is a "Test" class, the TestMethod itself is considered the test. A "test" class can effectively have many tests.

Peter Ritchie
  • 35,463
  • 9
  • 80
  • 98