I'm working on a significant number of integration tests in MSTest (100+ for a subsystem), and I really want to group related tests into their own test classes, for readability and because different test groups require different configuration setup.
The tests originally started out with one big test class, that contained static instances of certain services. These services take a while to spin up, and will dominate the test execution time if they are brought up and torn down for each test. The original test code started like this.
[TestClass]
public class IntegrationTests
{
static ServiceA serviceA = new ServiceA();
static ServiceB serviceB = new ServiceB();
[ClassInitialize]
public static void ClassInit(TestContext context)
{
/* Gets the services up and going */
}
[TestInitialize]
public void TestInit()
{
/* Gets services back into good states */
}
/* Lots and lots of Integration tests */
[ClassCleanup]
public static void ClassCleanup()
{
/* Tear down the services */
}
}
The problem with this is different test groups need the services put into different states before the tests are run.
My first idea was to keep the static services in a common base test class, and then create new test classes that inherit from the base class for each test group, like this:
[TestClass]
public class TestClassA : IntegrationTests
{
/* tests */
}
[TestClass]
public class TestClassB : IntegrationTests
{
/* tests */
}
[TestClass]
public class TestClassC : IntegrationTests
{
/* tests */
}
The problem with that is the services get spun up and torn down for each test class. Granted, that's not as bad as them getting created and destroyed for each individual test, but I would like to create them once for each test run.
Is there a way to have separate test classes that all share the same services for integration testing in MSTest?