gpt4 book ai didi

python - 有没有办法控制 pytest-xdist 如何并行运行测试?

转载 作者:IT老高 更新时间:2023-10-28 22:19:19 40 4
gpt4 key购买 nike

我有以下目录布局:

runner.py
lib/
tests/
testsuite1/
testsuite1.py
testsuite2/
testsuite2.py
testsuite3/
testsuite3.py
testsuite4/
testsuite4.py

testsuite*.py模块的格式如下:

导入pytest类测试东西:      def setup_class(self):          '''做一些设置'''          # 在这里做一些设置      def teardown_class(self):          ''''做一些拆解'''          # 在这里做一些拆解的东西      def test1(自我):          # 做一些与 test1 相关的事情      def test2(自我):          # 做一些与 test2 相关的事情      ……      ……      ……      def test40(自我):          # 做一些与 test40 相关的事情如果 __name__=='__main()__'   pytest.main(args=[os.path.abspath(__file__)])

我遇到的问题是我想并行执行“测试套件”,即我希望 testsuite1、testsuite2、testsuite3 和 testsuite4 开始并行执行,但测试套件中的各个测试需要串行执行。

当我使用 py.test 中的“xdist”插件并使用“py.test -n 4”启动测试时,py.test 正在收集所有测试并在 4 个工作人员之间随机负载平衡测试。这导致在“testsuitex.py”模块中每次测试时都要执行“setup_class”方法(这违背了我的目的。我希望每个类只执行一次 setup_class 并在之后连续执行测试)。

基本上我希望执行的样子是:

worker1:串行执行 testsuite1.py 中的所有测试worker2:串行执行 testsuite2.py 中的所有测试worker3:串行执行 testsuite3.py 中的所有测试worker4:串行执行 testsuite4.py 中的所有测试

worker1、worker2、worker3和worker4都是并行执行的。

有没有办法在 'pytest-xidst' 框架中实现这一点?

我能想到的唯一选择是启动不同的进程以在 runner.py 中单独执行每个测试套件:

<上一页>def test_execute_func(testsuite_path): subprocess.process('py.test %s' % testsuite_path)如果 __name__=='__main__': #收集所有测试套件名称 对于每个测试套件: multiprocessing.Process(test_execute_func,(testsuite_path,))

最佳答案

您可以使用 --dist=loadscope 将所有测试分组到同一个测试类中。这是来自 pytest-xdist on pypi 的文档

By default, the -n option will send pending tests to any worker that is available, without any guaranteed order, but you can control this with these options:

--dist=loadscope: tests will be grouped by module for test functions and by class for test methods, then each group will be sent to an available worker, guaranteeing that all tests in a group run in the same process. This can be useful if you have expensive module-level or class-level fixtures. Currently the groupings can’t be customized, with grouping by class takes priority over grouping by module. This feature was added in version 1.19.

--dist=loadfile: tests will be grouped by file name, and then will be sent to an available worker, guaranteeing that all tests in a group run in the same worker. This feature was added in version 1.21.

关于python - 有没有办法控制 pytest-xdist 如何并行运行测试?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4637036/

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