【发布时间】:2019-03-22 11:11:36
【问题描述】:
假设我有一个使用 pyserial 连接的测试子集。跳过相关的测试函数与文档中的 --runslow 示例基本相同。
我的问题是:我怎样才能跳过夹具,以免出现测试设置失败错误?也就是说,如果设备未连接,则会在夹具设置过程中引发错误。
EDIT(奖励)如果连接完全失败,可能会自动初始化跳过标记?
EDIT 2 我想我真的在问这两个TODOs 中的哪一个是“正确”的方法?
EDIT 3 或许只设置autouse=False 并确保每个使用fixture 的测试函数都被标记?
这是我正在尝试做的一个示例:
# conftest.py
import pytest
from typing import List
from serial import Serial
from _pytest.nodes import Item
from _pytest.config import Config
from _pytest.config.argparsing import Parser
def pytest_addoption(parser: Parser):
parser.addoption(
"--live",
action="store_true",
default=False,
help="utilize serial connection"
)
def pytest_collection_modifyitems(config: Config, items: List[Item]):
if config.getoption("--live"):
return
skip = pytest.mark.skip(
reason="needs '--live' option to run"
)
for item in items:
if "live" in item.keywords:
item.add_marker(skip)
# fixtures (port, etc.) for connection...
# TODO need to apply skip mark
@pytest.fixture(scope='session', autouse=True)
def serial_connect(port: str, ) -> Serial:
# TODO or catch and pass if config value is False
with Serial(port=port, ) as new:
yield new
【问题讨论】: