16-pytest-skip跳过用例

简介: 16-pytest-skip跳过用例

前言

  • 在自动化测试中,会有满足某个条件才执行某部分用例,否则跳过用例,这时可以使用skip来实现这个功能。

skip跳过测试方法

  • pytest.mark.skip(reason='原因')
  • 执行时加-r选项,展示跳过原因
1. # -*- coding: utf-8 -*-
2. # @Time    : 2021/10/24
3. # @Author  : 大海
4. # @File    : test_30.py
5. 
6. import pytest
7. 
8. 
9. # 直接跳过方法,reason 标记跳过原因,可省略   执行时加r选项,展示跳过原因
10. @pytest.mark.skip(reason='这是标记跳过用例的原因')
11. def test_login():
12. print('这是登录用例')
13. 
14. 
15. def test_logout():
16. print('这是登出用例')
17. 
18. 
19. class TestHomepage(object):
20. 
21. def test_one(self):
22. print('这是case1')
23. 
24. assert 1 == 1
25. 
26.     @pytest.mark.skip()
27. def test_two(self):
28. print('这是case2')
29. 
30. assert 2 == 2
31. 
32. 
33. if __name__ == '__main__':
34.     pytest.main(['-rs', 'test_30.py'])

skip跳过测试类型

1. # -*- coding: utf-8 -*-
2. # @Time    : 2021/10/24
3. # @Author  : 大海
4. 
5. 
6. import pytest
7. 
8. 
9. # 直接跳过测试类,reason 标记跳过原因,可省
10. @pytest.mark.skip()
11. class TestHomepage(object):
12. 
13. def test_one(self):
14. print('这是case1')
15. 
16. assert 1 == 1
17. 
18. def test_two(self):
19. print('这是case2')
20. 
21. assert 2 == 2
22. 
23. 
24. class TestMyPage(object):
25. 
26. def test_three(self):
27. print('这是case3')
28. 
29. assert 1 == 1
30. 
31. def test_four(self):
32. print('这是case4')
33. 
34. assert 2 == 2
35. 
36. 
37. if __name__ == '__main__':
38.     pytest.main(['-rs', 'test_31.py'])

skif满足条件跳过

1. # -*- coding: utf-8 -*-
2. # @Time    : 2021/10/24
3. # @Author  : 大海
4. # @File    : test_32.py
5. 
6. import pytest
7. 
8. app_version = 7.0
9. 
10. """
11. 1. @pytest.mark.skipif(1==1,reason='跳过一个方法或一个测试用例')
12. 2. 被标记的类或方法,条件为布尔值,当条件为ture时,会被跳过,否则不跳过
13. 3. reason 标记跳过原因,可省
14. """
15. 
16. 
17. @pytest.mark.skipif(app_version <= 8.0, reason='APP版本小于等于8.0时不执行')
18. class TestHomepage(object):
19. 
20. def test_one(self):
21. print('这是case1')
22. 
23. assert 1 == 1
24. 
25. def test_two(self):
26. print('这是case2')
27. 
28. assert 2 == 2
29. 
30. 
31. @pytest.mark.skipif(app_version > 8.0, reason='APP版本大于等于8.0时不执行')
32. class TestMyPage(object):
33. 
34. def test_three(self):
35. print('这是case3')
36. 
37. assert 1 == 1
38. 
39. def test_four(self):
40. print('这是case4')
41. 
42. assert 2 == 2
43. 
44. 
45. if __name__ == '__main__':
46.     pytest.main(['-rs', 'test_32.py'])

类和方法混合使用skipif

  • 多个skipif时,满足1个条件即跳过
1. # -*- coding: utf-8 -*-
2. # @Time    : 2021/10/24
3. # @Author  : 大海
4. # @File    : test_33.py
5. 
6. import pytest
7. 
8. app_version = 9.0
9. 
10. @pytest.mark.skipif(app_version <= 8.0, reason='APP版本小于等于8.0时不执行')
11. class TestHomepage(object):
12. 
13. def test_one(self):
14. print('这是case1')
15. 
16. assert 1 == 1
17. 
18.     @pytest.mark.skipif(app_version <= 9.0, reason='此用例APP版本小于等于9.0时不执行')
19. def test_two(self):
20. print('这是case2')
21. 
22. assert 2 == 2
23. 
24. 
25. if __name__ == '__main__':
26.     pytest.main(['-rs', 'test_33.py'])

skip多处调用

  • 变量名 = pytest.mark.skipif(条件, reason=原因)
1. # -*- coding: utf-8 -*-
2. # @Time    : 2021/10/24
3. # @Author  : 大海
4. # @File    : test_34.py
5. 
6. import pytest
7. 
8. app_version = 9.0
9. 
10. app_version_nine = pytest.mark.skipif(app_version <= 9.0, reason='此用例APP版本小于等于9.0时不执行')
11. 
12. 
13. @pytest.mark.skipif(app_version <= 8.0, reason='APP版本小于等于8.0时不执行')
14. class TestHomepage(object):
15. 
16. def test_one(self):
17. print('这是case1')
18. 
19. assert 1 == 1
20. 
21.     @app_version_nine
22. def test_two(self):
23. print('这是case2')
24. 
25. assert 2 == 2
26. 
27. 
28. if __name__ == '__main__':
29.     pytest.main(['-rs', 'test_34.py'])

方法内跳过

  • pytest.skip(reason='原因')
1. # -*- coding: utf-8 -*-
2. # @Time    : 2021/10/24
3. # @Author  : 大海
4. # @File    : test_35.py
5. 
6. import pytest
7. 
8. app_version = 9.0
9. 
10. 
11. @pytest.mark.skipif(app_version <= 8.0, reason='APP版本小于等于8.0时不执行')
12. class TestHomepage(object):
13. 
14. def test_one(self):
15. print('这是case1')
16.         pytest.skip('跳过此case')
17. assert 1 == 1
18. 
19. def test_two(self):
20. print('这是case2')
21. 
22. assert 2 == 2
23. 
24. 
25. if __name__ == '__main__':
26.     pytest.main(['-rs', 'test_35.py'])


相关文章
|
数据采集 运维 数据挖掘
API电商接口大数据分析与数据挖掘 (商品详情店铺)
API接口、数据分析以及数据挖掘在商品详情和店铺相关的应用中,各自扮演着重要的角色。以下是关于它们各自的功能以及如何在商品详情和店铺分析中协同工作的简要说明。
|
人工智能 数据挖掘 SEO
【真实体会】花几百块买ChatGPT4.0账号一年值得吗?
GPT4.0大幅提升内容质量和数量,支持文本、图片、文件问答,内置专业工具如AI设计助手。每月20美元,高效应用于SEO、写作、数据分析等场景,能自动修正错误,增强互动性,还可生成高质量视频。虽然官网有限制,移动APP无此问题。对比GPT3.5,GPT4是性价比高的多面工具,适合内容创作者。查看详细教程,参见个人主页介绍。
1453 0
【真实体会】花几百块买ChatGPT4.0账号一年值得吗?
|
安全 架构师 网络协议
微内核架构
微内核架构
|
前端开发 JavaScript 开发者
探究单页应用(SPA)与多页应用(MPA):技术的选择与对比
在当今快节奏的互联网时代,单页应用(SPA)和多页应用(MPA)是两种常见的前端开发架构。本文将深入探讨这两种应用类型的特点、优势和不足,并提供一些帮助开发者做出选择的指导。
|
网络架构
直通和交叉电缆:网络连接的桥梁
【10月更文挑战第15天】
800 5
|
物联网 Java 数据格式
阿里云物联网消息透传设备端payLoad设置问题
由于低配置且资源受限,或者对网络流量有要求的设备,不适合直接构造JSON数据与物联网平台通信,可将原数据透传到物联网平台。本文主要针对文档中未对设备端payLoad的设置进行介绍,初次使用容易出错,结合官方示例对payLoad对象的处理进行介绍。
14958 0
阿里云物联网消息透传设备端payLoad设置问题
|
消息中间件 存储 人工智能
RocketMQ 学习社区重磅上线!AI 互动,一秒了解 RocketMQ 功能源码
RocketMQ 学习社区重磅上线!AI 互动,一秒了解 RocketMQ 功能源码
|
机器学习/深度学习
CSS3 如何实现飘动的云朵动画
CSS3 如何实现飘动的云朵动画
469 0
|
存储 消息中间件 缓存
基于 MaxCompute 的实时数据处理实践
MaxCompute 通过流式数据高性能写入和秒级别查询能力(查询加速),提供EB级云原生数仓近实时分析能力;高效的实现对变化中的数据进行快速分析及决策辅助。当前Demo基于近实时交互式BI分析/决策辅助场景,实现指标卡近实时BI分析、近实时市场监测、近实时趋势分析、近实时销量拆分功能。
2123 1
基于 MaxCompute 的实时数据处理实践
|
Java 程序员 Maven
【Maven打包报错解决方案】Using ‘UTF-8‘ encoding to copy filtered resources.
【Maven打包报错解决方案】Using ‘UTF-8‘ encoding to copy filtered resources.
1019 0
【Maven打包报错解决方案】Using ‘UTF-8‘ encoding to copy filtered resources.