5

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

4

3 回答 3

5

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

不,所有测试都在同一个线程上运行

还是每个单独的测试都在它自己的线程中运行?

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

如果每个包含测试的类都有自己的线程,这些是同时运行的吗?即,A 类的测试会与 B 类的测试同时运行吗?或者 A 类的测试会运行(每个在自己的线程上),然后是 B 类的测试(每个在自己的线程上)?

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

于 2013-02-14T06:16:41.020 回答
3

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

我发现有趣的是 TestClass 为每个测试构建一次。我认为每个类只会构建一次。

此外,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
于 2013-02-14T06:09:43.243 回答
0

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

于 2014-06-26T09:42:48.723 回答