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

Pytest测试框架中存在两个跳过测试的方法:skip和skipif 。
(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
Pytest失败重试就是,在执行一次测试脚本时,如果一个测试用例执行结果失败了,则重新执行该测试用例。
- 现在我也找了很多测试的朋友,做了一个分享技术的交流群,共享了很多我们收集的技术文档和视频教程。
- 如果你不想再体验自学时找不到资源,没人解答问题,坚持几天便放弃的感受
- 可以加入我们一起交流。而且还有很多在自动化,性能,安全,测试开发等等方面有一定建树的技术大牛
- 分享他们的经验,还会分享很多直播讲座和技术沙龙
- 可以免费学习!划重点!开源的!!!
- qq群号:110685036【暗号:csdn999】

前提:
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今天的分享就到此结束了,点赞关注不迷路