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

Mee*_*han 5 pytest

我的测试脚本如下

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

def test_1()
    ...............

def test_2()
    ...............

def test_3()
 ...............
Run Code Online (Sandbox Code Playgroud)

顺序脚本执行工作正常。(第一个测试设置 -> Test1 -> Test2 ->Test3 -> Tear down)使用 pytest。

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

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

我尝试提供--dist=load选项,将安装和拆卸放入conftest文件等中。在我的情况下没有任何效果。

小智 -1

尝试--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")
Run Code Online (Sandbox Code Playgroud)

输出 -

C:\pytest_demo\test3>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
.
Run Code Online (Sandbox Code Playgroud)