如果测试文件具有设置和拆卸部分,如何使用pytest并行执行测试



我的测试脚本如下

@pytest.fixture(scope="Module", Autouse="True")
def setup_test():
....................
yield
............
def test_1()
...............
def test_2()
...............
def test_3()
...............

顺序脚本执行正常。(第一个测试设置->测试1->检测2->试验3->拆卸(使用pytest。

如何并行运行脚本执行,如:-第一次测试设置->并行的所有测试用例->拆除?

如果我在pytest执行中使用-n选项,它甚至在设置部分完成之前就并行触发所有测试,并在所有测试用例之后执行拆卸部分。

我尝试提供--dist=load选项;拆下conftest文件等。对我来说什么都不管用。

我尝试过--dist=loadscope,它是按顺序执行测试,而不是并行

import logging
import time
import pytest
@pytest.fixture(scope="session", autouse="True")
def create_test_setup():
logging.info("Setup start")
time.sleep(10)
logging.info("Setup ends")
yield
logging.info("Tear down start")
time.sleep(10)
logging.info("Tear down ends")
def test_1():
logging.info("Test1 start")
time.sleep(10)
logging.info("Test1 ends")
def test_2():
logging.info("Test2 start")
time.sleep(20)
logging.info("Test2 ends")
def test_3():
logging.info("Test3 start")
time.sleep(30)
logging.info("Test3 end")
def test_4():
logging.info("Test4 start")
time.sleep(40)
logging.info("Test4 ends")

pytest -n 4 --dist=loadscope
============================= test session starts ==============================
platform linux -- Python 3.6.8, pytest-5.2.0, py-1.9.0, pluggy-0.13.1 -- 
.......................................
...................................
scheduling tests via LoadScopeScheduling
test.py::test_1
test.py::test_2
test.py::test_3
test.py::test_4
======================== 4 passed in 136.40s (0:02:16) =========================
Process finished with exit code 0
[gw0] [ 25%] PASSED test.py::test_1 2020-07-14 19:10:29,359 - INFO [root] -  test.py:20 - Test1 start
2020-07-14 19:10:39,363 - INFO [root] -  test.py:22 - Test1 ends
[gw0] [ 50%] PASSED test.py::test_2 2020-07-14 19:10:39,377 - INFO [root] -  test.py:26 - Test2 start
2020-07-14 19:10:59,397 - INFO [root] -  test.py:28 - Test2 ends
[gw0] [ 75%] PASSED test.py::test_3 2020-07-14 19:10:59,413 - INFO [root] -  test.py:32 - Test3 start
2020-07-14 19:11:29,436 - INFO [root] -  test.py:34 - Test3 end
[gw0] [100%] PASSED test.py::test_4 2020-07-14 19:11:29,450 - INFO [root] -  test.py:38 - Test4 start
2020-07-14 19:12:09,486 - INFO [root] -  test.py:40 - Test4 ends
2020-07-14 19:12:09,576 - INFO [root] -  test.py:14 - Tear down start
2020-07-14 19:12:19,586 - INFO [root] -  test.py:16 - Tear down ends

PyTest不知道并行运行时您的测试依赖于设置夹具。要告诉PyTest测试使用的是哪个fixture,您需要为测试函数添加一个与fixture同名的参数,然后PyTest将执行魔术,并知道使用fixture函数来创建一个用作该参数的对象。并行测试时,自动使用是不够的;我想这可能被认为是并行模式下的一个错误,但我不确定。

@pytest.fixture(scope="Module", Autouse="True")
def setup_test():
...............
yield
...............
def test_1(setup_test):
...............
def test_2(setup_test):
...............
def test_3(setup_test):
...............

注意,因为每个并行过程工作者都有自己的";"范围";setup_test()固定装置将在每个作用域中创建一个,即使它被标记为Module。如果这是在管理可能发生冲突的资源(例如端口、数据库、容器等(,请小心。如果这是一个问题,那么它们就不能并行运行(但可以与其他不使用fixture的测试并行运行(。

Try-dist=loadscope

import pytest
import logging
logging.basicConfig(format='%(message)s')
@pytest.fixture(scope="session", autouse=True)
def setup_test():
logging.warning("Setup")
yield
logging.warning("Tear down")
def test_1():
logging.warning("Test1")
def test_2():
logging.warning("Test2")
def test_3():
logging.warning("Test3")

输出-

C:pytest_demotest3>pytest -n3 -s --dist=loadscope
================================================= test session starts =================================================
platform win32 -- Python 3.7.1, pytest-5.2.2, py-1.7.0, pluggy-0.13.0
rootdir: C:pytest_demo, inifile: pytest.ini
plugins: allure-pytest-2.8.6, arraydiff-0.3, doctestplus-0.2.0, forked-1.1.3, html-2.0.0, metadata-1.8.0, openfiles-0.3.1, remotedata-0.3.1, xdist-1.30.0
gw0 [3] / gw1 [3] / gw2 [3]
Setup
Test1
.Test2
.Test3
Tear down
.

最新更新