在 Visual Studio 中控制单元测试的执行顺序 [英] Controlling execution order of unit tests in Visual Studio

查看:46
本文介绍了在 Visual Studio 中控制单元测试的执行顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,我已经搜索完了关于这方面的好信息.我有一系列单元测试,它们调用一个静态类,一旦初始化,就会设置不能(或我不希望)更改的属性.

Okay, I'm done searching for good information on this. I have a series of Unit Tests that call a static class which, once initialized, sets properties that cannot (or I don't wish to) change.

我的问题是我无法强制执行测试运行的既定顺序.如果可以,我可以以一种可靠的方式设置静态属性的方式运行它们,并且我可以对它们进行断言,但不幸的是 Microsoft.VisualStudio.TestTools.UnitTesting 框架只是以看似随机的顺序运行它们.

My problem is I cannot enforce a set order for the tests to run. If I could, I could run them in such a way as the static properties would be set in a reliable way, and I could Assert on them, but unfortunately the Microsoft.VisualStudio.TestTools.UnitTesting framework just runs them in a seemingly random order.

所以,我发现了这个 http://msdn.microsoft.com/en-us/library/microsoft.visualstudio.testtools.unittesting.priorityattribute.aspx 它在备注部分说测试系统不使用此属性.它提供给用户用于自定义目的."嗯?那有什么好处呢?他们是否希望我编写自己的测试包装器来利用这个美妙的属性(如果我想达到那种程度的努力,我可以轻松地编写自己的属性......)

So, I found this http://msdn.microsoft.com/en-us/library/microsoft.visualstudio.testtools.unittesting.priorityattribute.aspx which says in the Remarks section "This attribute is not used by the test system. It is provided to the user for custom purposes." Huh? What good is it then? Do they expect me to write my own testing wrapper to take advantage of this fabulous attribute (of which I could easily write myself if I wanted to go to that level of effort...)

所以,足够的咆哮;最重要的是,有没有办法控制我的单元测试运行的顺序?

So, enough of the rant; Bottom line, is there a way to control the order my unit tests run?

[TestMethod]
[Priority(0)]

等等.似乎不起作用,这是有道理的,因为微软说它不会.

etc. does NOT seem to work, which makes sense, since Microsoft says it won't.

另外,请不要评论违反隔离".TestClass 隔离了我正在测试的内容,而不是单独的 TestMethods.无论如何,每个测试都可以独立运行就好了,只是它们不能以随机顺序一起运行,因为无法拆除静态类.

Also, please no comments about "violating isolation". The TestClass isolates what I am testing, not the individual TestMethods. Regardless, each test can be run independently just fine, they just can't be run together in a random order as there is no way to tear down the static class.

哦,我也知道有序测试".

Oh, I also know about "Ordered Test".

推荐答案

将您的测试合并到一个巨大的测试中即可.为了使测试方法更具可读性,您可以执行类似

Merge your tests into one giant test will work. To make the test method more readable, you can do something like

[TestMethod]
public void MyIntegratonTestLikeUnitTest()
{
    AssertScenarioA();

    AssertScenarioB();

    ....
}

private void AssertScenarioA()
{
     // Assert
}

private void AssertScenarioB()
{
     // Assert
}

实际上,您遇到的问题表明您可能应该提高实现的可测试性.

Actually the issue you have suggests you probably should improve the testability of the implementation.

这篇关于在 Visual Studio 中控制单元测试的执行顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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