MSTest: execution order of tests within the same TestClass
I need to enforce TestClass
order when executing tests with MSTest.
The order of TestClasses
and tests within each class can be random, but MSTest should not pick a test开发者_StackOverflow from another TestClass
until it is done executing ClassInitialize
, all tests in the class, and ClassCleanup
.
I have global AssemblyInitialize
and AssemblyCleanup
, therefore the following does not work, because it initializes the assembly for each test:
MSTest.exe /testcontainer:MyUnitTests.dll /resultsfile:report.trx /test:TestClass1 /test:TestClass2
I asked a similar question here, though it was not about test class execution order. Ordering tests can cause them to be brittle if the reason for the ordering is so that some sort of state can be setup/maintained. If this is the case with your tests, I would suggest instead writing them in a way that would be order-agnostic.
As regards your problem with the assembly-level code, a work around for the AssemblyInitialize
and AssemblyCleanup
can be as follows:
private int InitCount;
[AssemblyInitialize]
public static void Setup(TestContext context)
{
if (InitCount++ == 0) {
//Do Something
}
}
[AssemblyCleanup]
public static void Teardown()
{
if (--InitCount == 0) {
//Do something
}
}
Basically, you can force the assembly-level methods to fire only once.
精彩评论