pytest【marker标记】

本文介绍了pytest的标记机制,通过@ pytest.mark装饰器为测试函数添加smoking和get_testing等自定义标记,用于冒烟测试。标记可以结合逻辑表达式进行筛选,并且即使标记的测试函数位于不同文件中也会一起执行。标记应用在冒烟测试场景,便于筛选和组织测试用例。
摘要由CSDN通过智能技术生成
测试函数标记

pytest提供了标记机制,允许你使用marker对测试函数(测试用例)做标记,一个测试函数(测试用例)可以有多个marker,一个marker也可以用例标记多个测试函数(测试用例)。针对冒烟测试,我们可以使用marker标记,检查系统有没有重大缺陷,因为冒烟测试通常是不会包含全套测试的。

选定了两个测试函数(测试用例)来作为冒烟测试,首先我们在 test_run_pass() 、test_run_fail() 两个测试函数(测试用例)的头部添加装饰器 @pytest.mark.smoking; @pytest.mark.get_testing(注意:smoking;get_testing 标记并非pytest 内置的,是我自己在配置文件定义的)。

import pytest


@pytest.mark.smoking
def test_run_pass():
    expected = (1, 2, 3)
    assert expected == (1, 2, 3)

@pytest.mark.smoking
def test_run_fail():
    expected = (1, 2, 3)
    assert expected == (1, 2, 4)

运行展示结果如下:

osts\ReadPytest>pytest -v -m 'smoking' test_two.py
================================================================================== test session starts ===================================================================================
collected 2 items                                                                                                                                                                         

test_two.py::test_run_pass PASSED                                                                                                                                                   [ 50%]
test_two.py::test_run_fail FAILED                                                                                                                                                   [100%]

======================================================================================== FAILURES ========================================================================================
_____________________________________________________________________________________ test_run_fail ______________________________________________________________________________________

    @pytest.mark.smoking
    def test_run_fail():
        expected = (1, 2, 3)
>       assert expected == (1, 2, 4)
E       assert (1, 2, 3) == (1, 2, 4)
E         At index 2 diff: 3 != 4
E         Full diff:
E         - (1, 
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值