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

有帮助吗?

解决方案

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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top