首页 文章

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

提问于
浏览
21

我有以下目录布局:

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并行开始执行,但是测试套件中的单个测试需要连续执行 .

当我使用py.test中的'xdist'插件并使用'py.test -n 4'开始测试时,py.test正在收集所有测试并随机地在4个工作者之间 balancer 测试 . 这导致在'testsuitex.py'模块中每次测试时都会执行'setup_class'方法(这违背了我的目的 . 我希望每个类只执行一次setup_class,然后在那里连续执行测试) .

基本上我想让执行看起来像是:

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,))

1 回答

  • 12

    使用pytest-xdist,目前没有"per-file"或"per-test-suite"分布 . 实际上,如果每个文件的分发(例如,文件中的测试一次只能由一个工作者执行)会对你的用例有所帮助,我建议你在https://bitbucket.org/hpk42/pytest/issues?status=new&status=open和链接pytest问题跟踪器上提交一个功能问题回到你的好解释 .

    欢呼声,霍尔格

相关问题