You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

.NET Core中如何批量并行执行列表中指定ID的测试用例

一次性并行执行指定TestCategory的所有测试用例

方法一:拼接TestCategory过滤条件

dotnet test的--filter参数支持用|(逻辑或)拼接多个TestCategory条件,这样可以一次性指定所有需要执行的测试用例ID,无需逐个遍历执行。

实现步骤

  1. 将测试用例ID列表拼接成符合filter语法的字符串:
    比如列表{"tc1", "tc2", "tc3"}可拼接为TestCategory=tc1|TestCategory=tc2|TestCategory=tc3
  2. 构造完整的dotnet test命令执行即可

C#代码示例

List<string> caseIds = new List<string>() { "tc1", "tc2", "tc3" };
// 拼接过滤条件
string filter = string.Join("|", caseIds.Select(id => $"TestCategory={id}"));
// 构造执行命令(可通过Process类调用执行)
string command = $"dotnet test --filter \"{filter}\"";

方法二:配合并行执行配置

结合你的系统支持最多24个浏览器实例的特性,需要确保dotnet test和测试框架开启并行执行,最大化利用资源:

1. 命令行直接启用并行

在命令中添加--parallel参数,同时指定最大并行数匹配浏览器实例上限:

dotnet test --filter "TestCategory=tc1|TestCategory=tc2|TestCategory=tc3" --parallel -- MaxCpuCount=24

2. 通过配置文件持久化并行设置

在项目中添加Test.runsettings文件,配置并行规则:

<RunSettings>
  <RunConfiguration>
    <MaxCpuCount>24</MaxCpuCount>
    <ParallelizeTestCollections>true</ParallelizeTestCollections>
  </RunConfiguration>
</RunSettings>

执行时指定配置文件:

dotnet test --filter "TestCategory=tc1|TestCategory=tc2|TestCategory=tc3" --settings Test.runsettings

注意事项

  • 确保测试框架(如xUnit、NUnit)支持并行执行,且测试用例之间无共享状态,避免并行时出现冲突
  • 若测试用例依赖浏览器实例,需保证测试框架的并行策略与浏览器实例管理逻辑匹配,防止资源耗尽

内容的提问来源于stack exchange,提问作者smiles

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.20 22:54:31