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



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

每个包含测试的类都在自己的线程中运行吗?

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

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

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

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

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

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

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

此外,类清理在所有测试结束时运行。我以为每个类的清理都会在该类的最后一次测试完成后运行。

最后,它们都在同一个线程上运行!现在这是出乎意料的。在今天的多核心世界(我的钻机有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

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

相关内容

  • 没有找到相关文章

最新更新