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

2024-06-26 18:07:30 发布

您现在位置:Python中文网/ 问答频道 /正文

我有以下目录布局:

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

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

import pytest 
class testsomething:
      def setup_class(self):
          ''' do some setup '''
          # Do some setup stuff here      
      def teardown_class(self):
          '''' do some teardown'''
          # Do some teardown stuff here

      def test1(self):
          # Do some test1 related stuff

      def test2(self):
          # Do some test2 related stuff

      ....
      ....
      ....
      def test40(self):
          # Do some test40 related stuff

if __name__=='__main()__'
   pytest.main(args=[os.path.abspath(__file__)])

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

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

基本上,我希望执行过程是这样的:

worker1: executes all tests in testsuite1.py serially
worker2: executes all tests in testsuite2.py serially
worker3: executes all tests in testsuite3.py serially
worker4: executes all tests in testsuite4.py serially

worker1, worker2, worker3 and worker4 都是并行执行的。

在“pytest xidst”框架中有实现这一点的方法吗?

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


def test_execute_func(testsuite_path):
    subprocess.process('py.test %s' % testsuite_path)

if __name__=='__main__':
   #Gather all the testsuite names
   for each testsuite:
       multiprocessing.Process(test_execute_func,(testsuite_path,))

Tags: pytestselfdefsetuptestssomeall
3条回答

可以使用--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.

是的,有这样的方法,每个xdist版本的可用选项如下:

  • --dist=each:将所有测试发送到所有节点,因此每个测试都在每个节点上运行。
  • --dist=load:将收集到的测试分布到所有节点,以便每个测试 只跑一次。所有节点收集并提交测试套件 当收到(测试套件的)所有集合时,将验证它们是否 相同的收藏。然后集合被分成 块和块被提交给节点执行。
  • --dist=loadscope将收集到的测试分布到所有节点,以便运行每个测试 就一次。所有节点收集并提交测试列表 收到集合,并验证它们是相同的集合。 然后集合被分成工作单元,按测试范围分组, 这些工作单元被提交到节点。
  • --dist=loadfile将收集到的测试分布到所有节点,以便运行每个测试 就一次。所有节点收集并提交测试列表 收到集合,并验证它们是相同的集合。 然后集合被分成工作单元,按测试文件分组, 这些工作单元被提交到节点。

如果您需要任何进一步的信息,我建议您直接进入actual implementation of the schedulers并检查分发是如何完成的。

使用pytest xdist,目前没有“每个文件”或“每个测试套件”分发。实际上,如果每个文件分发(例如,一个文件中的测试一次最多只能由一个工作人员执行)已经有助于您的用例,我建议您在https://bitbucket.org/hpk42/pytest/issues?status=new&status=open向pytest issue tracker提交一个功能问题,并链接到此处的良好解释。

干杯,霍尔格

相关问题 更多 >