重复 mstest 测试运行多次
我的一些 mstest 单元测试有助于检测多线程竞争条件,因此它们在连续运行多次时最有用,但我只想针对特定的测试运行执行此操作 - 而不是始终如此。
有没有办法配置 mstest(最好在测试列表编辑器中)多次运行测试?
Some of my mstest unit tests help detect multi-threading race conditions, and as such they are most useful when run many times in a row, but I only want to do this for specific test runs -- not all the time.
Is there a way to configure mstest (in the Test List Editor preferably) to run a test multiple times?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我想答案是否定的。
I guess the answer is no.
您可以创建生成指定次数的数据行的自定义属性。 结果测试将被执行多次。 想法取自 https://learn.microsoft.com/en-us/visualstudio/test/how-to-create-a-data-driven-unit-test?view=vs-2022
示例属性类:
然后将此属性添加到测试方法中
You can create custom attribute that yields data rows specified number of times. As a result test will be executed several times. Idea is taken from https://learn.microsoft.com/en-us/visualstudio/test/how-to-create-a-data-driven-unit-test?view=vs-2022
Example attribute class:
And then add this attribute to a test method
我发现最简单的方法是使用“运行直至失败”(Ctr+R,U)。
该测试将循环运行,直到您停止它,同时显示已执行测试的状态。 它在菜单中的样子
The easiest way I found is to use Run Until Failure (Ctr+R,U).
This test will be run in loop until you stop it, meantime showing status of executed tests. how it looks like in menu
我需要做类似的事情,所以我想出了一个解决方案。
这并不简单,但是一旦所有内容都设置完毕,您就可以在项目中重复使用它。 我还在 GitHub 上下载了此代码 (https://github.com/johnkoerner/MSTestLooper) ,但以防万一这种情况在某个时候消失,我是这样做的。
首先,我们创建一个属性,将其应用于我们的类,以告诉它多次运行所有测试。 在单独的程序集中执行所有这些操作,因为 DLL 需要位于特殊位置。
接下来我们必须创建一个自定义测试类执行类:
最后我们添加一个自定义调用程序,这是我们执行循环的地方:
HelperTestResults 类只是构建输出字符串,您可以按照您想要的方式处理它,但我不希望包含该代码,因为它只会使这篇文章变得更长。
将其编译到 DLL 中,然后需要将其复制到
您还必须为该类创建一个注册表项:
现在您已经完成了所有这些操作,您终于可以使用该类了:
请注意,我们的测试方法接受一个参数,来自测试循环器。 我还使用数据驱动测试来展示这一点,以表明您可以将两者结合在一起以在数据集中生成大型排列。
I needed to do something similar, so I came up with a solution to this.
It's not simple, but once everything is setup you can reuse it across projects. I also have a download of this code on GitHub (https://github.com/johnkoerner/MSTestLooper), but in case that goes away at some point, here is how I did it.
First we create an attribute that we will apply to our class to tell it run all the tests multiple times. Do all of this in a separate assembly, because the DLL needs to live in a special location.
Next we have to create a custom test class execution class:
Finally we add a custom invoker, which is where we perform the looping:
The HelperTestResults class just builds up strings for output, you can handle this how you want and I don't want to include that code because it will just make this post that much longer.
Compile this into a DLL and then you need to copy it to
You also have to create a registry entry for the class:
Now that you have all of that done, you can finally use the class:
Notice that our test method accepts a parameter, which comes from the test looper. I also show this using a data driven test, to show you can combine the two together to generate large permutations across your data sets.
考虑创建一个测试来分离几个线程。 测试列表不允许您对同一测试有多个条目。 但是,您可以将多线程测试分配到其自己的列表,并仅在您想要运行该特定测试时调用它。
Consider creating a test to spin off a couple of threads. The Test List won't allow you to have multiple entries for the same test. You could assign, however, the multi-threaded test to its own list and call it only when you want to run that particular test.