pytest - 制作自定义测试结果
问题描述
是否可以添加与以下相同级别的自定义测试结果:通过、失败、xfail、跳过、取消选择?我希望能够在夹具设置期间分配自定义测试结果,类似于调用 pytest.skip()。
或者,是否可以从夹具中“取消选择”测试用例(即在夹具设置期间 - 不是收集阶段)?
我的问题
我使用 pytest不是为了测试 python 代码,而是为了对连接到运行 pytest 代码的主机 PC 的不同设备进行黑盒测试。在一个 pytest 会话期间,我只测试一个设备,但可能是多个接口(IP、RS-232、GPIO 等)。
启动 pytest 时,我提供了一个配置文件作为参数。该文件指定设备的 IP 地址以及主机 PC 已连接到设备的接口。我正在使用固定装置来跳过无法运行的测试,因为:
- 特定设备未实现测试用例的功能
- 主机不支持测试用例的功能(物理接口)
我需要能够区分跳过测试用例的这两个原因:在发布软件时,我需要确保没有因为主机配置而跳过的测试。因此,我想在前一种情况下将测试结果设置为“不适用”,在后一种情况下设置为“跳过”。
我可以在命令行上指定设备功能并让 pytest 收集阶段取消选择测试用例,但我真的需要/需要/希望在运行时完成。否则运行测试的参数数量将迅速增加。
我目前的测试结果如下所示:
- 设备 A:235 跳过了 468
- 设备 B:445 跳过 468
您可以看到我对这两种设备使用相同的测试套件,因为它们共享许多相同的功能(我只是没有编写许多验证共享功能的测试用例)。
它会是什么样子?
我希望能够像这样将测试标记为“不适用”:
@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()
解决方案
推荐阅读
- c# - 如何在 C# 类库中移动鼠标(没有 WPF 没有 WinForms)?
- javascript - 从平面结构和反向制作嵌套对象结构?
- scala - 如何在 Intellij IDEA ide 的一个依赖 jar 中导出所有依赖 jar?
- facebook - 如何在没有自由职业者业务验证的情况下公开 Facebook 聊天机器人?
- google-chrome - chrome pdf查看器下载html5 blob - 网络失败错误
- python - 如何在csv中读取直到字段数太大
- tomcat - tomcat8 - 400 的自定义错误页面
- reactjs - React,Redux - 通过 api 调用更新后获取数据
- glassfish - 无法向 Payara/Glassfish 部署战争。下面的日志文件片段
- java - 正确获取 Java/Gradle 项目中的资源