0

我正在使用 pytest 运行测试。

我想在一个模块中所有定义的测试之前运行一个前置条件,所以我创建了一个前置条件函数并用 pytest.fixturewith修饰它,scope = "module"因此它只会对模块中的所有测试执行一次。这个先决条件需要另一个夹具test_target来为测试做准备。除此之外,他们自己的测试功能也使用了test_target以及test_param从 yaml 文件中加载的另一个。 要测试的设备总是在包含要测试的目标信息的 devices.yaml 文件中定义,因此我正在参数化yaml 文件中加载的数据test_targettest_param

我遇到的问题是,我定义为在模块范围内运行的前提条件是在函数范围内运行。

我注意到,当我直接将字典分配给 test_target 以进行参数化时,前提条件的行为符合预期(模块范围),但是我需要对设备信息进行硬编码。

我究竟做错了什么?也许还有另一种解决方法来执行这个前提条件?我已经用 json 试过了,我的行为与 yaml 完全相同

我的设置使用:

  • WSL-Ubuntu 20.04
  • 蟒蛇3.9
  • pytest 6.2.5

我的示例测试树:

.
├── station.json
├── station.yaml
└── test
    ├── conftest.py
    ├── test_1.py
    └── test_2.py

1 directory, 5 files

站.yaml

my_station:
  target1: 12345
  param1: 67890

站.json

{
    "my_station": {
        "target1": 12345,
        "param1": 67890
    }
}

conftest.py

import time
import json
import pytest
import yaml


def pytest_generate_tests(metafunc):
    devices_file = "station.yaml"
    sut_name = "my_station"
    with open(devices_file,"r") as file:
        setups = yaml.safe_load(file)
        # setups = json.load(file)
    # setups = {
    #     "my_station": {
    #         "target1": 12345,
    #         "param1": 67890,
    #     }
    # }
    sut = setups.get(sut_name)
    if not sut:
        pytest.exit(
            f"{sut_name} configuration not found\
                        on file {devices_file}"
        )
    targets = [device for device in sut if device.startswith("target")]
    params = [device for device in sut if device.startswith("param")]
    if "test_target" in metafunc.fixturenames:
        metafunc.parametrize("test_target", targets, scope="module")
    if "test_param" in metafunc.fixturenames:
        metafunc.parametrize("test_param", params, scope="module")


@pytest.fixture(scope="module")
def precondition1(test_target):
    print(f"\nStart precondition1 \nSave current status of test target {test_target}")
    yield
    time.sleep(0.1)
    print(f"\nStop precondition1 \nRestore original status of test target {test_target}")

测试1.py

def test_foo(test_target, test_param, precondition1):
    assert True


def test_boo(test_target, test_param, precondition1):
    assert True


def test_bar(test_target, test_param, precondition1):
    assert True


def test_mar(test_target, test_param, precondition1):
    assert True

测试2.py

def test_nah(test_target, precondition1):
    assert True


def test_duh(test_target, precondition1):
    assert True


def test_bla(test_target, precondition1):
    assert True

在我得到这个输出之前,当我在代码中运行测试时

❯ pytest -s -v
======================================================================================================================== test session starts ========================================================================================================================
platform linux -- Python 3.9.5, pytest-6.2.5, py-1.11.0, pluggy-1.0.0 -- /bin/python3.9
cachedir: .pytest_cache
rootdir: /home/pedro/repos/pytest_fixture
collected 7 items                                                                                                                                                                                                                                                   

test/test_1.py::test_foo[target1-param1] 
Start precondition1 
Save current status of test target target1
PASSED
test/test_1.py::test_boo[target1-param1] 
Stop precondition1 
Restore original status of test target target1

Start precondition1 
Save current status of test target target1
PASSED
test/test_1.py::test_bar[target1-param1] 
Stop precondition1 
Restore original status of test target target1

Start precondition1 
Save current status of test target target1
PASSED
test/test_1.py::test_mar[target1-param1] 
Stop precondition1 
Restore original status of test target target1

Start precondition1 
Save current status of test target target1
PASSED
Stop precondition1 
Restore original status of test target target1

test/test_2.py::test_nah[target1] 
Start precondition1 
Save current status of test target target1
PASSED
test/test_2.py::test_duh[target1] 
Stop precondition1 
Restore original status of test target target1

Start precondition1 
Save current status of test target target1
PASSED
test/test_2.py::test_bla[target1] 
Stop precondition1 
Restore original status of test target target1

Start precondition1 
Save current status of test target target1
PASSED
Stop precondition1 
Restore original status of test target target1


========================================================================================================================= 7 passed in 0.74s =========================================================================================================================

而如果我直接运行测试并分配加载的值(在 conftest.py 中取消注释行)

    setups = {
        "my_station": {
            "target1": 12345,
            "param1": 67890,
        }
    }

我得到以下输出:

❯ pytest -s -v
======================================================================================================================== test session starts ========================================================================================================================
platform linux -- Python 3.9.5, pytest-6.2.5, py-1.11.0, pluggy-1.0.0 -- /bin/python3.9
cachedir: .pytest_cache
rootdir: /home/pedro/repos/pytest_fixture
collected 7 items                                                                                                                                                                                                                                                   

test/test_1.py::test_foo[target1-param1] 
Start precondition1 
Save current status of test target target1
PASSED
test/test_1.py::test_boo[target1-param1] PASSED
test/test_1.py::test_bar[target1-param1] PASSED
test/test_1.py::test_mar[target1-param1] PASSED
Stop precondition1 
Restore original status of test target target1

test/test_2.py::test_nah[target1] 
Start precondition1 
Save current status of test target target1
PASSED
test/test_2.py::test_duh[target1] PASSED
test/test_2.py::test_bla[target1] PASSED
Stop precondition1 
Restore original status of test target target1


========================================================================================================================= 7 passed in 0.26s =========================================================================================================================
4

0 回答 0