问题
Does each Class containing tests run in it's own thread? Or does each individual test run in it's own thread? If each Class containing tests has its own thread, are these run concurrently? ie, would tests from class A run concurrently with tests from Class B? Or would class A's tests run (each on their own thread), followed by tests from Class B (each on their own thread)?
回答1:
Does each Class containing tests run in it's own thread?
No. All tests run on the same thread
Or does each individual test run in it's own thread?
No. All tests run on the same thread.
If each Class containing tests has its own thread, are these run concurrently? ie, would tests from class A run concurrently with tests from Class B? Or would class A's tests run (each on their own thread), followed by tests from Class B (each on their own thread)?
If you are creating your threads. You are owning the concurrency model for them. The vstest executor does not run your concurrently. There is no guarantee on the sequence in which tests are executed, be it from within a given testclass or the sequence between different testclasses.
回答2:
Well it seems it does not apply threading at all! I built a test project that contains 3 unit tests, just outputting info to a file using Nlog.
What I found interesting is that the TestClass is constructed once for EACH test. I thought each class would only be constructed ONCE.
Also, the Class Cleanups run at the end of all the tests. I thought each class's cleanup would run after the last test for that class was done.
And finally, they are all run on the same thread! Now that is unexpected. In today's world of multi cores ( my rig has 4), I would have expected to see more threads in the picture.
Each test looks like this:
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();
}
}
}
This is the output...
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
回答3:
Also note that if your unit tests are thread safe, you do have the option to apply threading and have the tests execute in parallel. See this blog post from Microsoft.
来源:https://stackoverflow.com/questions/14860665/how-does-the-visual-studio-2012-test-runner-apply-threading