gpt4 book ai didi

c# - 在 MSTest 中,如何指定某些测试方法不能相互并行运行?

转载 作者:行者123 更新时间:2023-11-30 12:37:55 24 4
gpt4 key购买 nike

我有一大组测试网站服务器的集成测试。大多数这些测试都可以并行运行。但是,我有一些更改设置并且在并行运行时可能导致彼此失败。

作为一个简化的例子,假设我进行了这些测试:

TestPrice_5PercentTax
TestPrice_10PercentTax
TestPrice_NoTax
TestInventory_Add10Items
TestInventory_Remove10Items

库存测试不会互相干扰,也不受价格测试的影响。但是价格测试会改变 Tax 设置,所以如果 510 并行运行,10最终可能会在 5 完成之前更改设置,而 5 会失败,因为它看到 10% 的税而不是预期的 5%。

我想为三个价格测试定义一个类别,并说它们可能不会彼此同时运行。它们可以与任何其他测试同时运行,但不能与其他价格测试同时运行。有没有办法在 MSTest 中执行此操作?

最佳答案

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?;
}
}

可在此处找到更多详细信息MSTest v2 at meziantou.net

我强烈建议至少快速通读一下链接,因为这可能会帮助您解决和理解并行或顺序运行测试的问题。

关于c# - 在 MSTest 中,如何指定某些测试方法不能相互并行运行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57717251/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com