有没有办法用 MSTests 按顺序运行单元测试? [英] Is there a way to run unit tests sequentially with MSTests?

查看:26
本文介绍了有没有办法用 MSTests 按顺序运行单元测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个主要是单线程、单用户的应用程序中工作.这里和那里有一些工作线程,它们只使用线程安全的对象和类.单元测试实际上是用多线程测试那些(为测试明确创建的),并且它们测试得很好.

I am working in an application that is mostly single-thread, single user. There are a few worker threads here and there, and they are only using thread safe objects and classes. The unit tests are actually testing those with multiple threads (explicitly created for the tests), and they test fine.

在测试非线程安全的业务对象和子系统时,VSTS 单元测试失败.它们不是线程安全的也没关系,这就是应用程序使用它们的方式.

The VSTS unit tests fail when testing business objects and sub-systems that are not thread safe. It is okay for them not to be thread-safe, that's the way the application uses them.

但是 MS 测试的每个测试方法一个线程"方法让我们丧命.我不得不在许多单元测试类中实现对象锁只是为了确保测试一个接一个地运行(我并不真正关心顺序,但我不能让两个测试方法在同时).

But the 'one thread per TestMethod' approach of MS tests kills us. I've had to implement object locks in many unit test classes just to ensure that the tests are run one after the other (I don't really care about the order, but I can't have two test methods hitting the same object at the same time).

代码如下:

[TestClass]
public class TestSomeObject
{
   static object turnStile = new object();
...
   [TestMethod]
   public void T01_TestThis()
   {
      lock(turnStile)
      {
      .. actual test code
      }
   }

   [TestMethod]
   public void T02_TestThat()
   {
      lock(turnStile)
      {
      -- actual test code
      }
   }

}

有没有更好/更优雅的方式让测试按顺序运行?

Is there a better/more elegant way to make the test run sequentially?

推荐答案

有一个有序测试"的概念,您可以在其中按顺序列出测试.它更倾向于确保特定的顺序,但如果 B 不等待 A 完成,我看不出这怎么可能.

There is the notion of an "Ordered Test" in which you can list tests in sequence. It is more geared towards ensuring a certain sequential order, but I can't see how that would be possible if B doesn't wait for A to complete.

除此之外,不幸的是您的测试相互干扰.每个测试都可以使用设置/拆卸方法,以便最终可以将测试彼此隔离.

Apart from that, it is unfortunate that your tests interfere with each other. There are Setup / TearDown methods that can be used per test such that it may after all be possible to isolate the tests from each other.

这篇关于有没有办法用 MSTests 按顺序运行单元测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆