制作自定义测试结果

时间:2020-08-28 09:37:59

标签: pytest

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

或者,是否可以从固定装置中“取消选择”测试用例(即在固定装置安装过程中-而非收集阶段)?

我的问题

我正在使用pytest not 来测试python代码,但用于对连接到运行pytest代码的主机的不同设备进行黑盒测试。在一次pytest会话中,我只测试一台设备,但可能测试多个接口(IP,RS-232,GPIO等)。

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

  • 特定设备未实现测试用例的功能
  • 主机不支持测试用例的功能(物理接口)

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

我可以在命令行上指定设备功能,并让pytest收集阶段取消选择测试用例,但是我确实需要/要求/希望在运行时完成此操作。否则,用于运行测试的参数数量将迅速爆炸。

我当前的测试结果如下:

  • 设备A:从468个设备中跳出了235个
  • 设备B:从468个设备中跳出了445个

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

看起来如何?

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

@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()

0 个答案:

没有答案