前言
- 在自动化测试中,会有满足某个条件才执行某部分用例,否则跳过用例,这时可以使用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'])