前言
- 在自动化测试中,想要按自定义顺序执行测试用例,怎么办呢?这时就需要一个第三库( pytest-ordering)来实现。
pytest用例执行顺序
- 不同文件的执行顺序:按照目录文件名顺序执行
- 同一文件下的执行顺序:按照用例顺序从上到下执行
顺序执行
1. # -*- coding: utf-8 -*- 2. # @Time : 2021/10/23 3. # @Author : 大海 4. # @File : test_28.py 5. import pytest 6. 7. # 同一个文件中,按照用例顺序从上到下执行 8. class TestHomepage(object): 9. 10. 11. def test_2(self): 12. print('这是用例2') 13. 14. def test_1(self): 15. print('这是用例1') 16. 17. 18. 19. if __name__ == '__main__': 20. pytest.main(['-s','test_28.py'])
自定义顺序
- 安装依赖包:pip install pytest-ordering
1. # -*- coding: utf-8 -*- 2. # @Time : 2021/10/23 3. # @Author : 大海 4. # @File : test_29.py 5. 6. import pytest 7. 8. 9. # 使用装饰器@pytest.mark.run(order=num),按num数值顺序执行 10. class TestHomepage(object): 11. 12. @pytest.mark.run(order=2) 13. def test_2(self): 14. print('这是用例2') 15. 16. @pytest.mark.run(order=1) 17. def test_1(self): 18. print('这是用例1') 19. 20. 21. if __name__ == '__main__': 22. pytest.main(['-s', 'test_29.py'])