pytest测试框架系列 - Pytest skip和skipif 跳过用例看这篇就够了!

## 前言 - skip和skipif,看名字就是跳过测试的意思,主要用于不想执行的代码,标记后,标记的代码不执行。 - 希望满足某些条件才执行某些测试用例,否则pytest会跳过运行该测试用例 - 实际常见场景:根据平台不同执行测试、跳过依赖、功能未完成预期不能执行的测试 ## 有哪些用例跳过方式 - 跳过单个测试用例 - 跳过测试类 - 跳过测试模块 - 通过条件跳过执行case - 自定义跳过标识 ### 跳过单个测试用例 - @pytest.mark.skip(reason=" ") 跳过执行测试用例 示例: ```python # !/usr/bin/python3 # _*_coding:utf-8 _*_ """" # @Time  :2021/7/5 20:15 # @Author  : king # @File   :test_skip.py # @Software :PyCharm # @blog :https://blog.csdn.net/u010454117 # @WeChat Official Account: 【测试之路笔记】 """ import pytest @pytest.mark.skip(reason="跳过测试函数的测试case") def test_01(): print("我是测试函数。") assert 1 == 1 class TestSkip: @pytest.mark.skip(reason="跳过类里面case") def test_one(self, ): print("test_one方法执行") assert 1 == 1 def test_two(self): print("test_two方法执行") assert "king" in "hello,king" if __name__ == '__main__': pytest.main() ``` 执行结果: ![在这里插入图片描述](https://www.icode9.com/i/ll/?i=20210705204010351.png?,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3UwMTA0NTQxMTc=,size_16,color_FFFFFF,t_70) - pytest.skip(msg="") 在测试函数内部使用 示例: ```python # !/usr/bin/python3 # _*_coding:utf-8 _*_ """" # @Time  :2021/7/5 20:15 # @Author  : king # @File   :test_skip.py # @Software :PyCharm # @blog :https://blog.csdn.net/u010454117 # @WeChat Official Account: 【测试之路笔记】 """ import pytest def test_01(): print("我是测试函数。") pytest.skip(msg="跳过此条用例。") assert 1 == 1 class TestSkip: def test_one(self, ): print("test_one方法执行") assert 1 == 1 def test_two(self): pytest.skip(msg="跳过此条用例。") print("test_two方法执行") assert "king" in "hello,king" if __name__ == '__main__': pytest.main() ``` 执行结果: ![在这里插入图片描述](https://www.icode9.com/i/ll/?i=20210705204416129.png?,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3UwMTA0NTQxMTc=,size_16,color_FFFFFF,t_70) ### 跳过测试类 - @pytest.mark.skip(reason=" ") 示例: ```python # !/usr/bin/python3 # _*_coding:utf-8 _*_ """" # @Time  :2021/7/5 20:15 # @Author  : king # @File   :test_skip.py # @Software :PyCharm # @blog :https://blog.csdn.net/u010454117 # @WeChat Official Account: 【测试之路笔记】 """ import pytest def test_01(): print("我是测试函数。") assert 1 == 1 @pytest.mark.skip(reason="跳过下面测试类") class TestSkip: def test_one(self, ): print("test_one方法执行") assert 1 == 1 def test_two(self): print("test_two方法执行") assert "king" in "hello,king" if __name__ == '__main__': pytest.main() ``` 执行结果: ![在这里插入图片描述](https://www.icode9.com/i/ll/?i=20210705204747648.png?,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3UwMTA0NTQxMTc=,size_16,color_FFFFFF,t_70) ### 跳过测试模块 - pytestmark = pytest.mark.skip(reason="") pytestmark 不可更改变量名,让他等于标签即可 示例: ```python # !/usr/bin/python3 # _*_coding:utf-8 _*_ """" # @Time  :2021/7/5 20:15 # @Author  : king # @File   :test_skip.py # @Software :PyCharm # @blog :https://blog.csdn.net/u010454117 # @WeChat Official Account: 【测试之路笔记】 """ import pytest pytestmark = pytest.mark.skip(reason="跳过当前模块") def test_01(): print("我是测试函数。") assert 1 == 1 class TestSkip: def test_one(self, ): print("test_one方法执行") assert 1 == 1 def test_two(self): print("test_two方法执行") assert "king" in "hello,king" if __name__ == '__main__': pytest.main() ``` 执行结果: ![在这里插入图片描述](https://www.icode9.com/i/ll/?i=202107052051483.png?,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3UwMTA0NTQxMTc=,size_16,color_FFFFFF,t_70) - pytest.skip("", allow_module_level=True) 示例: ```python # !/usr/bin/python3 # _*_coding:utf-8 _*_ """" # @Time  :2021/7/5 20:15 # @Author  : king # @File   :test_skip.py # @Software :PyCharm # @blog :https://blog.csdn.net/u010454117 # @WeChat Official Account: 【测试之路笔记】 """ import pytest import sys if not sys.platform.startswith("win"): pytest.skip("下面用例仅在win下执行", allow_module_level=True) def test_01(): print("我是测试函数。") assert 1 == 1 class TestSkip: def test_one(self, ): print("test_one方法执行") assert 1 == 1 def test_two(self): print("test_two方法执行") assert "king" in "hello,king" if __name__ == '__main__': pytest.main() ``` 执行结果: ![在这里插入图片描述](https://www.icode9.com/i/ll/?i=20210705211912489.png?,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3UwMTA0NTQxMTc=,size_16,color_FFFFFF,t_70) ### 通过条件跳过执行case - @pytest.mark.skipif(condition="表达式", reason="") -- 若满足condition,则跳过测试函数 示例: ```python # !/usr/bin/python3 # _*_coding:utf-8 _*_ """" # @Time  :2021/7/5 20:37 # @Author  : king # @File   :test_skip_if.py # @Software :PyCharm # @blog :https://blog.csdn.net/u010454117 # @WeChat Official Account: 【测试之路笔记】 """ import pytest import sys @pytest.mark.skipif(sys.version_info < (3, 8), reason="python版本不低于3.8") def test_01(): print("我是测试函数。") assert 1 == 1 @pytest.mark.skipif(condition=sys.version_info < (3, 8), reason="python版本不低于3.8") class TestSkip: def test_one(self, ): print("test_one方法执行") assert 1 == 1 def test_two(self): print("test_two方法执行") assert "king" in "hello,king" if __name__ == '__main__': pytest.main() ``` 执行结果: ![在这里插入图片描述](https://www.icode9.com/i/ll/?i=2021070521071951.png?,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3UwMTA0NTQxMTc=,size_16,color_FFFFFF,t_70) ### 自定义跳过标识 - 使用myskip = pytest.mark.skip() 或 myskip = pytest.mark.skipif(condition=...)装饰时用该变量代替标签即可:@myskip 原因:如果多个case需要使用同一个原因时,可以少些很多代码,增强代码可读性 示例: ```python # !/usr/bin/python3 # _*_coding:utf-8 _*_ """" # @Time  :2021/7/5 20:37 # @Author  : king # @File   :test_skip_if.py # @Software :PyCharm # @blog :https://blog.csdn.net/u010454117 # @WeChat Official Account: 【测试之路笔记】 """ import pytest import sys mark_version = pytest.mark.skipif(sys.version_info < (3, 8), reason="python版本不低于3.8") # mark_version = pytest.mark.skip(reason="部分功能未完成") @mark_version def test_01(): print("我是测试函数。") assert 1 == 1 class TestSkip: def test_one(self, ): print("test_one方法执行") assert 1 == 1 @mark_version def test_two(self): print("test_two方法执行") assert "king" in "hello,king" if __name__ == '__main__': pytest.main() ``` 执行结果: ![在这里插入图片描述](https://www.icode9.com/i/ll/?i=20210705212614644.png?,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3UwMTA0NTQxMTc=,size_16,color_FFFFFF,t_70) ## 总结 - @pytest.mark.skip(reason="") 跳过测试用例执行,放在单个测试用例上跳过单个,放在类上面,跳过类里面的所有的用例,reason为选填参数 - @pytest.mark.skipif(condition=表达式, reason="") 跳过测试用例执行,放在单个测试用例上跳过单个,放在类上面,跳过类里面的所有的用例,reason为必填参数 - pytestmark = pytest.mark.skip(reason="") 或 pytest.skip(reason="", allow_module_level=True),跳过当时py文件里面的所有用例,格式不能改变 以上为内容纯属个人理解,如有不足,欢迎各位大神指正,转载请注明出处! >**如果觉得文章不错,欢迎关注微信公众号,微信公众号每天推送相关测试技术文章** >个人微信号:搜索 【测试之路笔记】
上一篇:IDC报告:2020年阿里云领跑中国金融云市场


下一篇:IDC:未来计算的十大特征(附链接)