Visual Studio 2012测试运行器如何应用线程?

时间:2013-02-13 18:41:28

标签: mstest visual-studio-test-runner

每个包含测试的类是否都在自己的线程中运行?或者每个单独的测试都在它自己的线程中运行?如果每个包含测试的类都有自己的线程,它们是否同时运行?即,来自A类的测试是否会与B类测试同时进行?或者是A类的测试运行(每个都在自己的线程上),然后是B类的测试(每个都在自己的线程上)?

3 个答案:

答案 0 :(得分:5)

  

每个包含测试的Class是否都在自己的线程中运行?

没有。所有测试都在同一个线程上运行

  

或者每个单独的测试都在自己的线程中运行?

没有。所有测试都在同一个线程上运行。

  

如果每个包含测试的类都有自己的线程,那么这些是否运行   同时?即,来自A类的测试将与测试同时运行   来自B级?或者是A类的测试运行(每个都在他们自己的线程上),   然后是B类测试(每个都在他们自己的线程上)?

如果您正在创建线程。您拥有它们的并发模型。 vstest执行程序不会同时运行。无法保证执行测试的顺序,无论是来自给定的测试类还是不同测试类之间的序列。

答案 1 :(得分:3)

好吧它似乎根本不适用线程!我构建了一个包含3个单元测试的测试项目,只是使用Nlog将信息输出到文件。

我发现有趣的是TestClass是为EACH测试构建的。我认为每个班级只会在一开始就构建。

此外,Class Cleanups在所有测试结束时运行。我认为在完成该课程的最后一次测试后,每个课程的清理工作都会进行。

最后,他们都在同一个线程上运行!现在这是出乎意料的。在今天的多核心世界中(我的装备有4个),我希望在图片中看到更多的线程。

每个测试都是这样的:

using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using NLog;
using System.Threading;

namespace SeeHowTestsUseThreads
{
    [TestClass]
    public class UnitTest1
    {
        static Logger logger = LogManager.GetCurrentClassLogger();

        public UnitTest1()
        {
            logger.Info("Thread {0}: Constructing UnitTest1", Thread.CurrentThread.ManagedThreadId);
        }

        [ClassInitialize()]
        public static void MyClassInitialize(TestContext testContext) {
            logger.Info("Thread {0}: UnitTest1 Class Initialize", Thread.CurrentThread.ManagedThreadId);
        }
        [ClassCleanup()]
        public static void MyClassCleanup() {
            logger.Info("Thread {0}: UnitTest1 Class Cleanup", Thread.CurrentThread.ManagedThreadId);
        }
        [TestInitialize()]
        public void MyTestInitialize() {
            logger.Info("Thread {0}: UnitTest1 Test Initialize", Thread.CurrentThread.ManagedThreadId);
        }

        [TestCleanup()]
        public void MyTestCleanup() {
            logger.Info("Thread {0}: UnitTest1 Test Cleanup", Thread.CurrentThread.ManagedThreadId);
        }

        [TestMethod]
        public void TestMethod1()
        {
            logger.Info("Thread {0}: UnitTest1 TestMethod1 Run", Thread.CurrentThread.ManagedThreadId);
            Assert.Inconclusive();
        }

        [TestMethod]
        public void TestMethod2()
        {
            logger.Info("Thread {0}: UnitTest1 TestMethod2 Run", Thread.CurrentThread.ManagedThreadId);
            Assert.Inconclusive();
        }
        [TestMethod]
        public void TestMethod3()
        {
            logger.Info("Thread {0}: UnitTest1 TestMethod3 Run", Thread.CurrentThread.ManagedThreadId);
            Assert.Inconclusive();
        }
        [TestMethod]
        public void TestMethod4()
        {
            logger.Info("Thread {0}: UnitTest1 TestMethod4 Run", Thread.CurrentThread.ManagedThreadId);
            Assert.Inconclusive();
        }
        [TestMethod]
        public void TestMethod5()
        {
            logger.Info("Thread {0}: UnitTest1 TestMethod5 Run", Thread.CurrentThread.ManagedThreadId);
            Assert.Inconclusive();
        }
        [TestMethod]
        public void TestMethod6()
        {
            logger.Info("Thread {0}: UnitTest1 TestMethod6 Run", Thread.CurrentThread.ManagedThreadId);
            Assert.Inconclusive();
        }
    }
}

这是输出......

2013-02-14 07:59:42.3879 INFO Thread 10: Constructing UnitTest3
2013-02-14 07:59:42.3979 INFO Thread 10: UnitTest3 Class Initialize
2013-02-14 07:59:42.3979 INFO Thread 10: UnitTest3 Test Initialize
2013-02-14 07:59:42.3979 INFO Thread 10: UnitTest3 TestMethod1 Run
2013-02-14 07:59:42.3979 INFO Thread 10: UnitTest3 Test Cleanup
2013-02-14 07:59:42.4220 INFO Thread 10: Constructing UnitTest3
2013-02-14 07:59:42.4220 INFO Thread 10: UnitTest3 Test Initialize
2013-02-14 07:59:42.4220 INFO Thread 10: UnitTest3 TestMethod2 Run
2013-02-14 07:59:42.4220 INFO Thread 10: UnitTest3 Test Cleanup
2013-02-14 07:59:42.4220 INFO Thread 10: Constructing UnitTest3
2013-02-14 07:59:42.4220 INFO Thread 10: UnitTest3 Test Initialize
2013-02-14 07:59:42.4220 INFO Thread 10: UnitTest3 TestMethod3 Run
2013-02-14 07:59:42.4220 INFO Thread 10: UnitTest3 Test Cleanup
2013-02-14 07:59:42.4220 INFO Thread 10: Constructing UnitTest3
2013-02-14 07:59:42.4220 INFO Thread 10: UnitTest3 Test Initialize
2013-02-14 07:59:42.4220 INFO Thread 10: UnitTest3 TestMethod4 Run
2013-02-14 07:59:42.4220 INFO Thread 10: UnitTest3 Test Cleanup
2013-02-14 07:59:42.4220 INFO Thread 10: Constructing UnitTest3
2013-02-14 07:59:42.4290 INFO Thread 10: UnitTest3 Test Initialize
2013-02-14 07:59:42.4290 INFO Thread 10: UnitTest3 TestMethod5 Run
2013-02-14 07:59:42.4290 INFO Thread 10: UnitTest3 Test Cleanup
2013-02-14 07:59:42.4290 INFO Thread 10: Constructing UnitTest3
2013-02-14 07:59:42.4290 INFO Thread 10: UnitTest3 Test Initialize
2013-02-14 07:59:42.4290 INFO Thread 10: UnitTest3 TestMethod6 Run
2013-02-14 07:59:42.4290 INFO Thread 10: UnitTest3 Test Cleanup
2013-02-14 07:59:42.4290 INFO Thread 10: Constructing UnitTest1
2013-02-14 07:59:42.4290 INFO Thread 10: UnitTest1 Class Initialize
2013-02-14 07:59:42.4290 INFO Thread 10: UnitTest1 Test Initialize
2013-02-14 07:59:42.4290 INFO Thread 10: UnitTest1 TestMethod1 Run
2013-02-14 07:59:42.4290 INFO Thread 10: UnitTest1 Test Cleanup
2013-02-14 07:59:42.4290 INFO Thread 10: Constructing UnitTest1
2013-02-14 07:59:42.4290 INFO Thread 10: UnitTest1 Test Initialize
2013-02-14 07:59:42.4290 INFO Thread 10: UnitTest1 TestMethod2 Run
2013-02-14 07:59:42.4290 INFO Thread 10: UnitTest1 Test Cleanup
2013-02-14 07:59:42.4290 INFO Thread 10: Constructing UnitTest1
2013-02-14 07:59:42.4290 INFO Thread 10: UnitTest1 Test Initialize
2013-02-14 07:59:42.4290 INFO Thread 10: UnitTest1 TestMethod3 Run
2013-02-14 07:59:42.4290 INFO Thread 10: UnitTest1 Test Cleanup
2013-02-14 07:59:42.4290 INFO Thread 10: Constructing UnitTest1
2013-02-14 07:59:42.4290 INFO Thread 10: UnitTest1 Test Initialize
2013-02-14 07:59:42.4290 INFO Thread 10: UnitTest1 TestMethod4 Run
2013-02-14 07:59:42.4290 INFO Thread 10: UnitTest1 Test Cleanup
2013-02-14 07:59:42.4290 INFO Thread 10: Constructing UnitTest1
2013-02-14 07:59:42.4290 INFO Thread 10: UnitTest1 Test Initialize
2013-02-14 07:59:42.4290 INFO Thread 10: UnitTest1 TestMethod5 Run
2013-02-14 07:59:42.4290 INFO Thread 10: UnitTest1 Test Cleanup
2013-02-14 07:59:42.4440 INFO Thread 10: Constructing UnitTest1
2013-02-14 07:59:42.4440 INFO Thread 10: UnitTest1 Test Initialize
2013-02-14 07:59:42.4440 INFO Thread 10: UnitTest1 TestMethod6 Run
2013-02-14 07:59:42.4440 INFO Thread 10: UnitTest1 Test Cleanup
2013-02-14 07:59:42.4440 INFO Thread 10: Constructing UnitTest2
2013-02-14 07:59:42.4440 INFO Thread 10: UnitTest2 Class Initialize
2013-02-14 07:59:42.4440 INFO Thread 10: UnitTest2 Test Initialize
2013-02-14 07:59:42.4440 INFO Thread 10: UnitTest2 TestMethod1 Run
2013-02-14 07:59:42.4440 INFO Thread 10: UnitTest2 Test Cleanup
2013-02-14 07:59:42.4440 INFO Thread 10: Constructing UnitTest2
2013-02-14 07:59:42.4440 INFO Thread 10: UnitTest2 Test Initialize
2013-02-14 07:59:42.4440 INFO Thread 10: UnitTest2 TestMethod2 Run
2013-02-14 07:59:42.4440 INFO Thread 10: UnitTest2 Test Cleanup
2013-02-14 07:59:42.4440 INFO Thread 10: Constructing UnitTest2
2013-02-14 07:59:42.4440 INFO Thread 10: UnitTest2 Test Initialize
2013-02-14 07:59:42.4440 INFO Thread 10: UnitTest2 TestMethod3 Run
2013-02-14 07:59:42.4440 INFO Thread 10: UnitTest2 Test Cleanup
2013-02-14 07:59:42.4440 INFO Thread 10: Constructing UnitTest2
2013-02-14 07:59:42.4440 INFO Thread 10: UnitTest2 Test Initialize
2013-02-14 07:59:42.4440 INFO Thread 10: UnitTest2 TestMethod4 Run
2013-02-14 07:59:42.4440 INFO Thread 10: UnitTest2 Test Cleanup
2013-02-14 07:59:42.4600 INFO Thread 10: Constructing UnitTest2
2013-02-14 07:59:42.4600 INFO Thread 10: UnitTest2 Test Initialize
2013-02-14 07:59:42.4600 INFO Thread 10: UnitTest2 TestMethod5 Run
2013-02-14 07:59:42.4600 INFO Thread 10: UnitTest2 Test Cleanup
2013-02-14 07:59:42.4600 INFO Thread 10: Constructing UnitTest2
2013-02-14 07:59:42.4600 INFO Thread 10: UnitTest2 Test Initialize
2013-02-14 07:59:42.4600 INFO Thread 10: UnitTest2 TestMethod6 Run
2013-02-14 07:59:42.4600 INFO Thread 10: UnitTest2 Test Cleanup
2013-02-14 07:59:42.4600 INFO Thread 10: UnitTest3 Class Cleanup
2013-02-14 07:59:42.4600 INFO Thread 10: UnitTest1 Class Cleanup
2013-02-14 07:59:42.4600 INFO Thread 10: UnitTest2 Class Cleanup

答案 2 :(得分:0)

另请注意,如果您的单元测试是线程安全的,您可以选择应用线程并让测试并行执行。请参阅此blog post from Microsoft