MSTest 如何确定测试方法的运行顺序?

发布于 2024-08-21 12:52:56 字数 656 浏览 4 评论 0原文

编辑:注意,问题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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

野侃 2024-08-28 12:52:56

我相信 MSTest 执行测试方法,按“ID”(似乎是它们的完整命名空间)对它们进行排序。

我创建了一个 TestProject1,其中包含 4 个单元测试(UnitTest1、...2、...A、...B)。每个单元测试包含 5 个测试方法(TestMethodA、...B、...1、...2、...3)。它们是在测试类中以随机顺序声明的。现在,每次运行 MSTest 时,测试都会以相同的顺序执行:

TestProject1.UnitTest1.TestMethod1
TestProject1.UnitTest1.TestMethod2
TestProject1.UnitTest1.TestMethod3
TestProject1.UnitTest1.TestMethodA
TestProject1.UnitTest1.TestMethodB
TestProject1.UnitTest2.TestMethod1
TestProject1.UnitTest2.TestMethod2
TestProject1.UnitTest2.TestMethod3
TestProject1.UnitTest2.TestMethodA
TestProject1.UnitTest2.TestMethodB
TestProject1.UnitTestA.TestMethod1
TestProject1.UnitTestA.TestMethod2
TestProject1.UnitTestA.TestMethod3
TestProject1.UnitTestA.TestMethodA
TestProject1.UnitTestA.TestMethodB
TestProject1.UnitTestB.TestMethod1
TestProject1.UnitTestB.TestMethod2
TestProject1.UnitTestB.TestMethod3
TestProject1.UnitTestB.TestMethodA
TestProject1.UnitTestB.TestMethodB

更改该顺序的唯一方法是重命名一个 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:

TestProject1.UnitTest1.TestMethod1
TestProject1.UnitTest1.TestMethod2
TestProject1.UnitTest1.TestMethod3
TestProject1.UnitTest1.TestMethodA
TestProject1.UnitTest1.TestMethodB
TestProject1.UnitTest2.TestMethod1
TestProject1.UnitTest2.TestMethod2
TestProject1.UnitTest2.TestMethod3
TestProject1.UnitTest2.TestMethodA
TestProject1.UnitTest2.TestMethodB
TestProject1.UnitTestA.TestMethod1
TestProject1.UnitTestA.TestMethod2
TestProject1.UnitTestA.TestMethod3
TestProject1.UnitTestA.TestMethodA
TestProject1.UnitTestA.TestMethodB
TestProject1.UnitTestB.TestMethod1
TestProject1.UnitTestB.TestMethod2
TestProject1.UnitTestB.TestMethod3
TestProject1.UnitTestB.TestMethodA
TestProject1.UnitTestB.TestMethodB

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.

生生不灭 2024-08-28 12:52:56

至于VSTest执行顺序。以下是它在 TestProject 中的组织方式:

  1. 按创建时间对项目中的 cs 文件进行排序 ASC
  2. 方法 每个文件中的位置

例如,项目中有 3 个 cs 文件。

  • UnitTest1.cs - 使用方法 TestMethod05 和 TestMethod03 创建于 01/01/1970
  • UnitTest2.cs - 使用方法 TestMethod02 创建于 05/01/1970。
  • UnitTest3.cs - 使用 TestMethod01 方法于 1970 年 3 月 1 日创建。

然后执行测试的顺序是这样的:

    TestProject1.UnitTest1.TestMethod05
    TestProject1.UnitTest1.TestMethod03
    TestProject1.UnitTest3.TestMethod01
    TestProject1.UnitTest2.TestMethod02

您可以使用命令查看“默认顺序”:

vstest.console.exe TestProject1.dll /ListTests

As for VSTest execution order. Here is how it's organized in your TestProject:

  1. Sort cs-files in your project by their CREATION Time ASC
  2. Method Position in each file

For example, you have 3 cs files in project.

  • UnitTest1.cs - created 01/01/1970 with methods TestMethod05 and TestMethod03
  • UnitTest2.cs - created 05/01/1970 with method TestMethod02.
  • UnitTest3.cs - created 03/01/1970 with method TestMethod01.

Then order of executing test is this:

    TestProject1.UnitTest1.TestMethod05
    TestProject1.UnitTest1.TestMethod03
    TestProject1.UnitTest3.TestMethod01
    TestProject1.UnitTest2.TestMethod02

You can see the 'default order' using command:

vstest.console.exe TestProject1.dll /ListTests

番薯 2024-08-28 12:52:56

Microsoft 文档

来自 NUnit:按优先级排序

 [Test, Order(5)]

对于 MSTest:使用 MSTest,测试会自动按测试名称排序。

注意:您也可以使用 自定义播放列表

From Microsoft's Document

For NUnit: Order by priority

 [Test, Order(5)]

For MSTest: With MSTest, tests are automatically ordered by their test name.

Note: Also you can use custom playlists

只是我以为 2024-08-28 12:52:56

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文