在 MSTest 中跨测试 类 共享服务

Sharing services across test classes in MSTest

我正在 MSTest 中进行大量集成测试(一个子系统超过 100 个),我真的很想将相关测试分组到它们自己的测试中 classes,以提高可读性并且因为不同测试组需要不同的配置设置。

测试最初是从一项大型测试 class 开始的,其中包含某些服务的静态实例。这些服务需要一段时间才能启动,如果它们在每次测试中启动和关闭,将占据测试执行时间。原来的测试代码是这样开始的

[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 */
  }
}

问题是不同的测试组需要在测试之前将服务置于不同的状态 运行。

我的第一个想法是将静态服务保留在公共基础测试 class 中,然后为每个测试组创建从基础 class 继承的新测试 classes ,像这样:

[TestClass]
public class TestClassA : IntegrationTests
{
  /* tests */
}

[TestClass]
public class TestClassB : IntegrationTests
{
  /* tests */
}

[TestClass]
public class TestClassC : IntegrationTests
{
  /* tests */
}

问题是每次测试都会启动和关闭服务 class。当然,这并不像为每个单独的测试创建和销毁它们那么糟糕,但我想为每个测试创建一次它们 运行.

有没有办法让单独的测试 classes 在 MSTest 中共享相同的集成测试服务?

如果我对你的问题理解正确,你可以使用单独的单例来实现这些服务。类似于:

public class Services
{
  static ServiceA _serviceA
  static ServiceB _serviceB
  public static ServiceA InstanceA
  {
    if (_serviceA == null)
      _serviceA = new ServiceA();
    return _serviceA;
  }
  public static ServiceB InstanceB
  {
    if (_serviceB == null)
      _serviceB = new ServiceB();
    return _serviceB;
  }
}

然后这个 class 可以被你所有的测试 class 共享。

IProgrammer 的回答可以,但这是我想出的解决方案。

我在基础测试 class 中添加了一个静态构造函数,其中所有服务都已启动,如下所示:

[TestClass]
public class IntegrationTests
{
  static ServiceA serviceA = new ServiceA();
  static ServiceB serviceB = new ServiceB();

  //Static constructor gets called once during the lifetime of the appdomain.
  static IntegrationTests()
  {
    /* Gets the services up and going */
  }
  [TestInitialize]
  public void TestInit()
  {
    /* Gets services back into good states */
  }

  /* Lots and lots of Integration tests */
}

在所有测试后清理资源 运行 有点棘手,因为 C# 没有静态析构函数。

但是,this answer provided 一个相当干净和聪明的解决方法。

这里是 "static" 析构函数代码。

private static readonly Destructor Finalise = new Destructor();
private sealed class Destructor
{
  ~Destructor()
  {
    /* Service cleanup code here */
  }
}