我有以下目录布局:
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个工作人员之间平衡测试。这样就可以在“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和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,))