我有大量的集成测试来测试网站服务器。大多数这些测试都可以并行运行。但是,我有一些更改设置,并且在并行运行时可能会导致彼此失败。
作为一个简化的示例,假设我进行了以下测试:
TestPrice_5PercentTax
TestPrice_10PercentTax
TestPrice_NoTax
TestInventory_Add10Items
TestInventory_Remove10Items
Run Code Online (Sandbox Code Playgroud)
库存测试不会互相妨碍,并且不受价格测试的影响。但价格测试将改变Tax设置,因此,如果两者5并行10运行,最终可能会在完成10之前更改设置,并且会失败,因为它看到了 10% 的税,而不是预期的 5%。55
我想为三个价格测试定义一个类别,并说它们可能不会同时运行。它们可以与任何其他测试同时运行,但不能与其他价格测试同时运行。MSTest 有办法做到这一点吗?
小智 8
MsTest v2 具有以下功能
[assembly: Parallelize(Workers = 0, Scope = ExecutionScope.MethodLevel)]
// Notice the assembly bracket, this can be compatible or incompatible with how your code is built
namespace UnitTestProject1
{
[TestClass]
public class TestClass1
{
[TestMethod]
[DoNotParallelize] // This test will not be run in parallel
public void TestPrice_5PercentTax() => //YourTestHere?;
[TestMethod]
[DoNotParallelize] // This test will not be run in parallel
public void TestPrice_10PercentTax() => //YourTestHere?;
[TestMethod]
[DoNotParallelize] // This test will not be run in parallel
public void TestPrice_NoTax() => //YourTestHere?;
[TestMethod]
public void TestInventory_Add10Items() => //YourTestHere?;
[TestMethod]
public void TestInventory_Remove10Items() => //YourTestHere?;
}
}
Run Code Online (Sandbox Code Playgroud)
更详细的信息可以在这里找到MSTest v2 at meziantou.net
我强烈建议至少快速阅读该链接,因为这可能会帮助您解决和理解并行或顺序运行的测试的问题。