SpecFlow与xUnit – 如何创建测试集合以防止测试的并行执行?

huangapple go评论43阅读模式
英文:

SpecFlow with xUnit - How to create test collections to prevent parallel execution of tests?

问题

我有一个测试项目(.Net 7),其中包含许多 SpecFlow 特性。在构建时,这些特性的 *.cs 文件会生成。据我所知,告诉 xUnit 以顺序而不是并行方式运行测试的唯一方法是将属性附加到测试类上。但由于这些类在每次构建时都是新生成的,所以这种方法不起作用。

我的测试是端到端测试,必须按顺序运行。我该如何做到这一点?

英文:

I have a test project (.Net 7) with a bunch of SpecFlow features. Upon build the *.cs files for those features are generated. As far as I know there is only one way to tell xUnit to run tests in sequence instead of running them parallel and that is by attaching attributes to the test classes. Since they are generated anew with every build that doesn't work.

My tests are end-to-end tests and must be run in sequence. How do I do that?

答案1

得分: 1

在文档页面https://xunit.net/docs/running-tests-in-parallel中描述了防止并行执行的几种方法:

将所有测试类默认放入单个测试集合中:
[assembly: CollectionBehavior(CollectionBehavior.CollectionPerAssembly)]
默认:CollectionBehavior.CollectionPerClass

设置并行运行测试时要使用的线程的最大数量:
[assembly: CollectionBehavior(MaxParallelThreads = n)]
默认:PC中的虚拟CPU数量

在程序集内部关闭并行执行:
[assembly: CollectionBehavior(DisableTestParallelization = true)]
默认:false

这些操作可以在手动添加到测试项目的类中执行,因此不会生成一个始终会被新生成的自动生成文件。

对于控制台运行器,有一个选项

-parallel none

对于MSBuild运行器,可以将属性ParallelizeAssemblies设置为true或MaxParallelThreads设置为1。

或者根据https://xunit.net/docs/configuration-files#maxParallelThreads中的说明创建/修改一个XUnit配置文件用于程序集。

英文:

On the documentation page https://xunit.net/docs/running-tests-in-parallel several ways of preventing parallel execution are described:

>Put all test classes into a single test collection by default:
> [assembly: CollectionBehavior(CollectionBehavior.CollectionPerAssembly)]
>Default: CollectionBehavior.CollectionPerClass
>Set the maximum number of threads to use when running test in parallel:
> [assembly: CollectionBehavior(MaxParallelThreads = n)]
>Default: number of virtual CPUs in the PC
>Turn off parallelism inside the assembly:
> [assembly: CollectionBehavior(DisableTestParallelization = true)]
>Default: false

These could be done in a class that you manually add once to the test project, so it won't be an autogenerated file that is always newly generated.

For the console runner, there is an option

-parallel none

For the MSBuild runner, you can set the property ParallelizeAssemblies to true or MaxParallelThreads to 1.

Or create / modify an XUnit configuration file for the assembly as described in https://xunit.net/docs/configuration-files#maxParallelThreads.

huangapple
  • 本文由 发表于 2023年2月9日 03:33:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/75390871.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定