英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论