C# 有没有办法用 MSTests 按顺序运行单元测试?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1544827/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is there a way to run unit tests sequentially with MSTests?
提问by Sam Dahan
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.
我在一个主要是单线程、单用户的应用程序中工作。这里和那里有一些工作线程,它们只使用线程安全的对象和类。单元测试实际上是在测试具有多个线程的那些(为测试明确创建的),并且它们测试良好。
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.
VSTS 单元测试在测试非线程安全的业务对象和子系统时失败。它们不是线程安全的也没关系,这就是应用程序使用它们的方式。
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).
但是 MS 测试的“每个测试方法一个线程”方法让我们丧命。我不得不在许多单元测试类中实现对象锁定,以确保测试一个接一个地运行(我并不真正关心顺序,但我不能让两个测试方法在同时)。
The code looks like this:
代码如下所示:
[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?
有没有更好/更优雅的方法让测试按顺序运行?
采纳答案by flq
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.
有一个“有序测试”的概念,您可以在其中按顺序列出测试。它更倾向于确保特定的顺序,但如果 B 不等待 A 完成,我看不出这怎么可能。
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.
除此之外,不幸的是您的测试相互干扰。每个测试都可以使用设置/拆卸方法,以便最终可以将测试彼此隔离。
回答by Robert Harvey
回答by Sam Dahan
I finally used the ordered test method. It works well.
我终于使用了有序的测试方法。它运作良好。
However, I had a hell of a time making it work with the NAnt build. Running onlythe ordered test list in the build requires using the /testmetadata and /testlist switches in the MSTest invocation block. The documentation on these is sketchy, to use a kind description. I google all over for examples of "MSTest /testmetadata /testlist" to no effect.
然而,我花了很长时间让它与 NAnt 构建一起工作。在构建中仅运行有序测试列表需要使用 MSTest 调用块中的 /testmetadata 和 /testlist 开关。关于这些的文档是粗略的,使用一种亲切的描述。我到处搜索“MSTest /testmetadata /testlist”的例子,但没有效果。
The trick is simple, however, and I feel compelled to give it back to the community, in case someone else bumps into the same issue.
然而,诀窍很简单,我觉得有必要把它回馈给社区,以防其他人遇到同样的问题。
- Edit the test metadata file (with a .vsmdi extension), and add a new list to the list of tests (the first node in the tree on the left pane. Give it the name you want, for example 'SequentialTests'.
- If you used a /testcontainer switch for the MSTest invocation, remove it.
- Add a switch for MSTest -> /testmetadata:
- Add a switch for MSTEst /testlist:SequentialTests (or whatever name you used)
- 编辑测试元数据文件(带有 .vsmdi 扩展名),并将一个新列表添加到测试列表(左窗格中树中的第一个节点。为其指定您想要的名称,例如“SequentialTests”。
- 如果您为 MSTest 调用使用了 /testcontainer 开关,请将其删除。
- 为 MSTest -> /testmetadata 添加一个开关:
- 为 MSTest /testlist:SequentialTests(或您使用的任何名称)添加一个开关
Then MSTest runs only the tests listed in the test list you created.
然后 MSTest 仅运行您创建的测试列表中列出的测试。
If someone has a better method, I'd like to hear about it!
如果有人有更好的方法,我想听听!
回答by Colin Dabritz
You can specifically require a mutex for each test execution, either in the specific tests you want to serialize, or for all the tests in a class (whatever shares the same mutex string).
您可以为每个测试执行特别要求一个互斥锁,无论是在您要序列化的特定测试中,还是在一个类中的所有测试中(任何共享相同的互斥锁字符串)。
For an entire test class, you can use the TestInitialize and TestCleanup attributes like so:
对于整个测试类,您可以使用 TestInitialize 和 TestCleanup 属性,如下所示:
private readonly Mutex testMutex = new Mutex(true, "MySpecificTestScenarioUniqueMutexString");
[TestInitialize]
public void Initialize()
{
testMutex.WaitOne(TimeSpan.FromSeconds(1));
}
[TestCleanup]
public void Cleanup() {
testMutex.ReleaseMutex();
}
To be clear this isn't a feature of tests, ANY locking structure should work. I'm using the system provided Mutexes in this case: https://msdn.microsoft.com/en-us/library/system.threading.mutex(v=vs.110).aspx
要清楚这不是测试的功能,任何锁定结构都应该工作。在这种情况下,我使用系统提供的互斥锁:https://msdn.microsoft.com/en-us/library/system.threading.mutex( v=vs.110).aspx
回答by Girish Sakhare
I used ordered tests, also configured them easily on jenkins just use command
我使用了有序的测试,也很容易在 jenkins 上配置它们,只需使用命令
MSTest /testcontainer:"orderedtestfilename.orderedtest" /resultsfile:"testresults.trx"
MSTest /testcontainer:"orderedtestfilename.orderedtest" /resultsfile:"testresults.trx"
回答by HB MAAM
you can Use Playlist
你可以使用播放列表
right click on the test method -> Add to playlist -> New playlist
右键单击测试方法 -> 添加到播放列表 -> 新建播放列表
you can then specify the execution order
然后您可以指定执行顺序