MSTest 如何确定测试方法的运行顺序?
编辑:注意,问题288805是类似的,但是,我特别问MSTest如何选择默认测试顺序。请参阅这个问题的其余部分。感谢 Eilon 提供的链接。
我正在查看一个遗留的 MSTest 项目。 测试始终以相同的顺序运行。 该订单不是按字母顺序排列的并且是弹跳的 两个 *.cs TestMethod 文件中的方法之间。
我没有改变遗产的物理顺序 代码。为了方便起见,我附加了“MSTest01” 到第一个测试的方法名称,“MSTest02”到 第二个测试的方法名称等。
令我惊讶的是,TestMethod 的执行顺序 功能发生变化; #3 第一,#6 第二,#5 第三, 等等。
当我从 TestMethod 函数名称及其执行顺序 改回之前的顺序,即 第一个 .cs 文件中的一个测试,来自第一个 .cs 文件的两个测试 第二个 .cs 文件,比第一个文件进行了五次测试 .cs 文件等。
看来文件位置可能不是一个因素 而 TestMethod 函数名称可能是一个因素。
问题:谁能解释一下 MSTest 如何决定 TestMethod 函数的执行顺序?
edit: note, question 288805 is similar, however, I specifically am asking how does MSTest choose the default test order. Please see the rest of this question. Thank you Eilon for the link.
I was looking at a legacy MSTest project.
The tests were always running in the same order.
The order was not alphabetic and was bouncing
between methods in two *.cs TestMethod files.
I did not change the physical order of the legacy
code. I did for my convenience append "MSTest01"
to the method name of the first test, "MSTest02" to the
method name of the second test, et cetera.
To my surprise, the execution order of the TestMethod
functions changed; #3 first, #6 second, #5 third,
et cetera.
When I removed the "MSTestnn" strings from the
TestMethod function names, their execution order
changed back to the previous ordering, i.e.,
one test from the first .cs file, two tests from
the second .cs file, five tests from the first
.cs file, et cetera.
It seems that file location may not be a factor
while TestMethod function name may be a factor.
QUESTION: can anyone explain how MSTest decides on execution order of TestMethod functions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我相信 MSTest 执行测试方法,按“ID”(似乎是它们的完整命名空间)对它们进行排序。
我创建了一个 TestProject1,其中包含 4 个单元测试(UnitTest1、...2、...A、...B)。每个单元测试包含 5 个测试方法(TestMethodA、...B、...1、...2、...3)。它们是在测试类中以随机顺序声明的。现在,每次运行 MSTest 时,测试都会以相同的顺序执行:
更改该顺序的唯一方法是重命名一个 TestClass 或 TestMethod。例如,如果我将 UnitTest1 的 TestMethodB 重命名为 TestMethod4,它将在 TestMethodA 之前执行。
要查看测试方法的 ID,请从 VS 打开“测试视图”窗口,然后右键单击列标题(例如测试名称)--> “添加/删除列...”并添加“ID”列。
I believe that MSTest executes test methods ordering them by their 'ID' (seems to be their full namespace).
I created a TestProject1 which contains 4 unit tests (UnitTest1, ...2, ...A, ...B). Each unit test contains 5 test methods (TestMethodA, ...B, ...1, ...2, ...3). They were declared with random order inside their test classes. Now, every time I run MSTest, the tests are executed with the same order:
The only way to change that order is to rename one TestClass or a TestMethod. If for example I rename the TestMethodB, of the UnitTest1, to TestMethod4 it will be executed before TestMethodA.
To see the IDs of your test methods open the 'Test View' window from VS and then right click on a column header (e.g. Test Name) --> "Add/Remove Columns..." and add 'ID' column.
至于VSTest执行顺序。以下是它在 TestProject 中的组织方式:
例如,项目中有 3 个 cs 文件。
然后执行测试的顺序是这样的:
您可以使用命令查看“默认顺序”:
As for VSTest execution order. Here is how it's organized in your TestProject:
For example, you have 3 cs files in project.
Then order of executing test is this:
You can see the 'default order' using command:
Microsoft 文档
来自 NUnit:按优先级排序
对于 MSTest:使用 MSTest,测试会自动按测试名称排序。
注意:您也可以使用 自定义播放列表
From Microsoft's Document
For NUnit: Order by priority
For MSTest: With MSTest, tests are automatically ordered by their test name.
Note: Also you can use custom playlists
MSTest 中创建的测试按执行时间升序排列,位于未失败的测试之后,因此随机执行。除非方法名称按字母顺序排列。
The tests created in MSTest are ordered by execution time, in ascending order, after the tests that have not failed, for this reason the random execution. Unless the method names are in alphabetical order.