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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2255284/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-07 00:47:45  来源:igfitidea点击:

how does MSTest determine the order in which to run test methods?

c#mstest

提问by gerryLowry

edit:note, question 288805 is similar, however, I specifically am asking how does MSTest choose the defaulttest order. Please see the rest of this question. Thank you Eilon for the link.

编辑:注意,问题 288805 是类似的,但是,我特别询问 MSTest 如何选择默认测试顺序。请参阅此问题的其余部分。谢谢 Eilon的链接

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.

我在看一个遗留的 MSTest 项目。测试总是以相同的顺序运行。该顺序不是按字母顺序排列的,而是在两个 *.cs TestMethod 文件中的方法之间跳动。

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.

我没有改变遗留代码的物理顺序。为了方便起见,我将“MSTest01”附加到第一个测试的方法名称,将“MSTest02”附加到第二个测试的方法名称,等等。

To my surprise, the execution order of the TestMethod functions changed; #3 first, #6 second, #5 third, et cetera.

令我惊讶的是,TestMethod 函数的执行顺序发生了变化;#3第一,#6第二,#5第三,等等。

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.

当我从 TestMethod 函数名称中删除“MSTestnn”字符串时,它们的执行顺序改回以前的顺序,即第一个 .cs 文件中的一个测试,第二个 .cs 文件中的两个测试,第一个 .cs 文件中的五个测试。 cs 文件等。

It seems that file location may not be a factor while TestMethod function name maybe a factor.

似乎文件位置可能不是一个因素,而 TestMethod 函数名称可能是一个因素。

QUESTION: can anyone explain how MSTest decides on execution order of TestMethod functions?

问题:谁能解释 MSTest 如何决定 TestMethod 函数的执行顺序?

回答by chaliasos

I believe that MSTest executes test methods ordering them by their 'ID' (seems to be their full namespace).

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

I created a TestProject1 wich contains 4 unt 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,其中包含 4 个 unt 测试(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

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.

更改该顺序的唯一方法是重命名一个 TestClass 或一个 TestMethod。例如,如果我将 UnitTest1 的 TestMethodB 重命名为 TestMethod4,它将在 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.

要查看测试方法的 ID,请从 VS 打开“测试视图”窗口,然后右键单击列标题(例如测试名称)-->“添加/删除列...”并添加“ID”列。

回答by Kim Ki Won

回答by Ivan Tsyng

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

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

  1. Sort cs-files in your project by their CREATION Time ASC
  2. Method Position in each file
  1. 按创建时间 ASC 对项目中的 cs 文件进行排序
  2. 每个文件中的方法位置

For example, you have 3 cs files in project.

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

  • 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.
  • UnitTest1.cs - 使用方法 TestMethod05 和 TestMethod03 创建 01/01/1970
  • UnitTest2.cs - 使用方法 TestMethod02 创建 05/01/1970。
  • UnitTest3.cs - 使用方法 TestMethod01 创建 03/01/1970。

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

vstest.console.exe TestProject1.dll /ListTests