【自动化测试】Pytest框架 —— 跳过测试和失败重试

1、Pytest跳过测试用例

自动化测试执行过程中,我们常常出现这种情况:因为功能阻塞,未实现或者环境有问题等等原因,一些用例执行不了, 如果我们注释掉或删除掉这些测试用例,后面可能还要进行恢复操作,这时我们就可以配置跳过这些用例。

Pytest测试框架中存在两个跳过测试的方法:skipskipif

(1)无条件跳过skip

skip方法为无条件跳过测试用例。

使用方法:@pytest.mark.skip标记在需要跳过的测试用例上。

(2)有条件跳过skipif

skipif方法为有条件跳过测试用例,条件为真跳过。

使用方法:@pytest.mark.skipif(condition=跳过的条件, reason=跳过的原因),

标记在需要符合条件跳过的测试用例上。

  • 参数condition:跳过的条件,为True则跳过测试,为False则继续执行测试,默认为True。
  • 参数reason:标注跳过的原因,必填参数。

(3)练习

"""
1.学习目标
    掌握pytest中跳过测试方法
2.操作步骤
    skipif(condition=判断条件,reason=跳过原因)
    使用时放置在需要跳过的用例之前
    @pytest.mark.skipif(条件,原因)  # 当条件为真,跳过执行
3.需求
"""
# 导入pytest
import pytest


# 编写测试用例
def login_data():
    return "jerry", "123456"


# 无条件跳过
@pytest.mark.skip
def test_register():
    """注册用例"""
    print("注册步骤")
    assert False


# 当条件为真,跳过测试
@pytest.mark.skipif(login_data()[0] == "jerry", reason="jerry用户不存在")
def test_login():
    """不记住密码登录"""
    username = login_data()[0]
    password = login_data()[1]
    print(f"输入用户名{username}")
    print(f"输入密码{password}")
    print("点击登录按钮")
    assert username == "jerry"


def test_shopping():
    """购物下单"""
    print("购物流程")
    assert True


if __name__ == '__main__':
    pytest.main()

"""
执行结果:跳过一个用例 : 1通过,2跳过

test_pytest_01.py::test_register 
test_pytest_01.py::test_login 
test_pytest_01.py::test_shopping 

======================== 1 passed, 2 skipped in 0.04s =========================

Process finished with exit code 0
SKIPPED (unconditional skip)
Skipped: unconditional skip
SKIPPED (jerry用户不存在)
Skipped: jerry用户不存在
购物流程
PASSED
"""
# 注:跳过的用例测试结果标识为s

2、Pytest失败重试

Pytest失败重试就是,在执行一次测试脚本时,如果一个测试用例执行结果失败了,则重新执行该测试用例。

前提:

Pytest测试框架失败重试需要下载pytest-rerunfailures插件。

安装方式:pip install pytest-rerunfailures

Pytest实现失败重试的方式:

方式一:在命令行或者main()函数中使用

pytest.main(['-vs','test_a.py','--reruns=2'])(这种方式没有实现成功,可能自己环境的问题)

或者:

pytest -vs ./test_a.py --reruns 2 --reruns-delay 2(可以)

表示:失败重试2次,在每次重试前会等到2秒。

说明: reruns为重跑次数,reruns_delay为间隔时间,单位s

方式二:在pytest.ini配置文件中使用(推荐)

pytest.ini配置文件中addopts添加reruns重试参数

[pytest]
addopts = -s --reruns 2 --reruns-delay 2
testpaths = scripts
python_files = test_01.py
python_classes = Test*
python_functions = test*

示例:使用第二种方式:

"""
1.学习目标
    掌握pytest中用例失败重试方法
2.操作步骤
    2.1 安装 pytest-rerunfailures
        pip install pytest-rerunfailures
    2.2 使用 在pytest.ini文件中,添加一个命令行参数  
        --reruns n # n表示重试次数
3.需求
"""
# 1.导入pytest
import pytest


# 2.编写测试用例
@pytest.mark.run(order=2)
def test_login():
    """登录用例"""
    print("登录步骤")
    assert "abcd" in "abcdefg"


@pytest.mark.run(order=1)
def test_register():
    """注册用例"""
    print("注册步骤")
    assert False


@pytest.mark.run(order=4)
def test_shopping():
    """购物下单"""
    print("购物流程")
    assert True


@pytest.mark.run(order=3)
def test_cart():
    """购物车用例"""
    print("购物车流程")
    assert True


if __name__ == '__main__':
    pytest.main(['-vs', 'test_01.py', '--reruns=2'])

# pytest ./pytest_demo/test_01.py --reruns 10 --reruns-delay 1
#
"""
执行结果:注意有两个:2 rerun
==================== 1 failed, 3 passed, 2 rerun in 0.09s =====================

test_01.py::test_register 注册步骤
RERUN
test_01.py::test_register 注册步骤
RERUN
test_01.py::test_register 注册步骤
FAILED
pytest_demo\test_01.py:20 (test_register)
@pytest.mark.run(order=1)
    def test_register():
        ""注册用例""
        print("注册步骤")
>       assert False
E       assert False

test_01.py:25: AssertionError
登录步骤
PASSED购物车流程
PASSED购物流程
PASSED
"""

注意:如果设置失败重试5次,在重试的过程中成功了,就不用全部跑完5次重试,这里需要注意一下。 


END绵薄之力

最后感谢每一个认真阅读我文章的人,看着粉丝一路的上涨和关注,礼尚往来总是要有的,虽然不是什么很值钱的东西,如果你用得到的话可以直接拿走:

这些资料,对于【软件测试】的朋友来说应该是最全面最完整的备战仓库,这个仓库也陪伴上万个测试工程师们走过最艰难的路程,希望也能帮助到你!

加入我的软件测试交流群:110685036免费获取~(同行大佬一起学术交流,每晚都有大佬直播分享技术知识点)

软件测试面试小程序

被百万人刷爆的软件测试题库!!!谁用谁知道!!!全网最全面试刷题小程序,手机就可以刷题,地铁上公交上,卷起来!

涵盖以下这些面试题板块:

1、软件测试基础理论 ,2、web,app,接口功能测试 ,3、网络 ,4、数据库 ,5、linux

6、web,app,接口自动化 ,7、性能测试 ,8、编程基础,9、hr面试题 ,10、开放性测试题,11、安全测试,12、计算机基础

获取方式 :

猜你喜欢

转载自blog.csdn.net/jiangjunsss/article/details/130642266
今日推荐