首页 > 解决方案 > 制作自定义测试结果

问题描述

是否可以添加与以下相同级别的自定义测试结果:通过、失败、xfail、跳过、取消选择?我希望能够在夹具设置期间分配自定义测试结果,类似于调用 pytest.skip()。

或者,是否可以从夹具中“取消选择”测试用例(即在夹具设置期间 - 不是收集阶段)?

我的问题

我使用 pytest不是为了测试 python 代码,而是为了对连接到运行 pytest 代码的主机 PC 的不同设备进行黑盒测试。在一个 pytest 会话期间,我只测试一个设备,但可能是多个接口(IP、RS-232、GPIO 等)。

启动 pytest 时,我提供了一个配置文件作为参数。该文件指定设备的 IP 地址以及主机 PC 已连接到设备的接口。我正在使用固定装置来跳过无法运行的测试,因为:

我需要能够区分跳过测试用例的这两个原因:在发布软件时,我需要确保没有因为主机配置而跳过的测试。因此,我想在前一种情况下将测试结果设置为“不适用”,在后一种情况下设置为“跳过”。

我可以在命令行上指定设备功能并让 pytest 收集阶段取消选择测试用例,但我真的需要/需要/希望在运行时完成。否则运行测试的参数数量将迅速增加。

我目前的测试结果如下所示:

您可以看到我对这两种设备使用相同的测试套件,因为它们共享许多相同的功能(我只是没有编写许多验证共享功能的测试用例)。

它会是什么样子?

我希望能够像这样将测试标记为“不适用”:

@pytest.fixture
def device(myconfig):
    cfg = myconfig.get_ip()
    return Device(cfg.ip, cfg.port)


@pytest.fixture
def serial(device, myconfig):
    if not device.has_serial:
        mark_not_applicable("Device does not support: serial")

    try:
        cfg = myconfig.get_serial()
        return serial.Serial(cfg.device, baudrate=cfg.baudrate)
    except:
        pytest.skip("Host interface not defined for: serial")


@pytest.fixture
def has_alpha(device):
    if not device.has_alpha:
        mark_not_applicable("Device does not support: alpha")


@pytest.fixture
def has_beta(device):
    if not device.has_beta:
        mark_not_applicable("Device does not support: beta")


def test_serial_protocol_alpha(serial, has_alpha):
    check_serial_protocol_alpha()


def test_serial_protocol_beta(serial, has_beta):
    check_serial_protocol_beta()

标签: pytest

解决方案


推荐阅读