同时运行多个pytest文件并行执行。

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

Run multiple pytest file parallelly

问题

在我的父文件夹中,有一个名为tests的子文件夹,其中包含所有的pytest文件。例如

..../tests/test_abc.py
.../tests/test_xyz.py

每个pytest文件中大约有8到10个测试用例。我想要并行运行这两个文件,因为按顺序执行它们会花费很多时间,以执行20个测试案例。

英文:

In my parent folder there is a subfolder tests and all pytest files are inside these. For example

..../tests/test_abc.py
.../tests/test_xyz.py

There are about 8 to 10 test cases in each of the pytest files. I want to run both these files parallelly as executing one after another it's taking a lot of time, to execute 20 cases.

答案1

得分: 1

你特别想要使用pytest-xdist的"--dist=loadfile"参数,以便通过文件来并行拆分测试。根据文档的说明:"测试被按照它们所在的文件分组。这些分组作为整体单元分配给可用的工作进程。这保证了文件中的所有测试在同一个工作进程中运行。"

因此,你首先需要运行以下命令来安装pytest-xdist:

pip install pytest-xdist

然后像这样运行你的测试:

pytest -n2 --dist=loadfile
英文:

You specifically want to use the --dist=loadfile arg of pytest-xdist in order to split tests in parallel via file. From the documentation: Tests are grouped by their containing file. Groups are distributed to available workers as whole units. This guarantees that all tests in a file run in the same worker.

So you would first pip install pytest-xdist, and then run your tests like this:

pytest -n2 --dist=loadfile

huangapple
  • 本文由 发表于 2023年6月19日 19:19:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/76506117.html
匿名

发表评论

匿名网友

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

确定