簡體   English   中英

pytest 添加選項以跳過夾具設置

[英]pytest add option to skip fixture setup

假設我有一個使用 pyserial 連接的測試子集。 跳過相關的測試函數與文檔中的--runslow示例大致相同。

我的問題是:如何跳過夾具以免出現測試設置失敗錯誤? 也就是說,如果設備未連接,則在夾具設置期間會引發錯誤。

如果連接完全失敗,編輯(獎金)可能會自動初始化跳過標記?

編輯 2我想我真的在問這兩個TODO的哪一個是“正確”的方法?

編輯 3也許只是設置autouse=False並確保每個使用夾具的測試函數都被標記?

這是我正在嘗試做的一個例子:

# 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

這個實現成功了; 希望得到反饋:

# conftest.py

import sys
import pytest

from typing import Optional
from serial import Serial, SerialException, \
    EIGHTBITS, PARITY_NONE, STOPBITS_ONE

from _pytest.config import UsageError
from _pytest.config.argparsing import Parser


def pytest_addoption(parser: Parser):

    parser.addoption(
        "--live", action="store_true", default=False,
        help="utilize serial connection for associated tests"
    )

    parser.addoption(
        "--port", action="store", type="string", default=None,
        help="serial port for establishing connection"
    )

    parser.addoption(
        "--timeout", action="store", type="float", default=None,
        help="port timeout for serial connection"
    )


@pytest.fixture
def live(request) -> bool:
    return request.config.getoption('live')


@pytest.fixture
def port(request) -> str:

    value = request.config.getoption('port')

    if value is not None:
        return value

    elif sys.platform.startswith('darwin'):
        return "/dev/tty.SLAB_USBtoUART"

    elif sys.platform.startswith('linux'):
        return "/dev/ttyACM0"

    elif sys.platform.startswith('win32'):
        return "COM14"

    else:
        pytest.skip("unspecified '--port' option")


@pytest.fixture
def timeout(request) -> Optional[float]:
    return request.config.getoption('timeout')


@pytest.fixture
def connection(live: bool, port: str, timeout: Optional[float]) -> Serial:

    if not live:
        pytest.skip("needs '--live' option to run")

    try:
        with Serial(
            port=port,  baudrate=460800, bytesize=EIGHTBITS,
            parity=PARITY_NONE, stopbits=STOPBITS_ONE, timeout=timeout
        ) as s:
            yield s

    except SerialException as e:
        raise UsageError(
            "invoked '--live' option, but connection failed"
        ) from e

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM