前言
正常情况下,pytest 用例默认执行顺序是自上而下的,对于一些有上下文依赖关系的用例,我们可以通过 pytest-ordering 插件来控制用例执行顺序,也可以通过setup、teardown和fixture来解决
pytest-ordering 详解 (建议掌握程度:☆☆)
安装
- 在命令行窗口输入:
pip install pytest-ordering
- 查看安装版本:
pip show pytest-ordering
使用
使用方法 : @pytest.mark.run(order=1)
未设置顺序示例:
# !/usr/bin/python3# _*_coding:utf-8 _*_""""
# @Time :2021/7/9 23:03
# @Author : king
# @File :test_order.py
# @Software :PyCharm
# @blog :https://blog.csdn.net/u010454117
# @WeChat Official Account: 【测试之路笔记】
"""import pytestdeftest_order01():print("我是 test_order01 用例")assertTruedeftest_order02():print("我是 test_order02 用例")assertTruedeftest_order03():print("我是 test_order03 用例")assertTrueif __name__=='__main__':
pytest.main(["-s"])
执行结果:从执行结果看是从上往下执行
pytest-ordering
示例:
# !/usr/bin/python3# _*_coding:utf-8 _*_""""
# @Time :2021/7/9 23:03
# @Author : king
# @File :test_order.py
# @Software :PyCharm
# @blog :https://blog.csdn.net/u010454117
# @WeChat Official Account: 【测试之路笔记】
"""import pytest@pytest.mark.run(order=3)deftest_order01():print("我是 test_order01 用例")assertTrue@pytest.mark.run(order=2)deftest_order02():print("我是 test_order02 用例")assertTrue@pytest.mark.run(order=1)deftest_order03():print("我是 test_order03 用例")assertTrueif __name__=='__main__':
pytest.main(["-s"])
执行结果:从下面结果可以看出,执行顺序按照设置的顺序执行
同时存在pytest-ordering
和未加顺序
用例示例:
# !/usr/bin/python3# _*_coding:utf-8 _*_""""
# @Time :2021/7/9 23:03
# @Author : king
# @File :test_order.py
# @Software :PyCharm
# @blog :https://blog.csdn.net/u010454117
# @WeChat Official Account: 【测试之路笔记】
"""import pytestdeftest_order001():print("我是 test_order001 用例")assertTrue@pytest.mark.run(order=3)deftest_order01():print("我是 test_order01 用例")assertTrue@pytest.mark.run(order=2)deftest_order02():print("我是 test_order02 用例")assertTrue@pytest.mark.run(order=1)deftest_order03():print("我是 test_order03 用例")assertTruedeftest_order04():print("我是 test_order04 用例")assertTrueif __name__=='__main__':
pytest.main(["-s"])
执行结果:用例先执行加了排序装饰器的,剩余按照默认顺序执行
总结
pytest 框架
用例默认情况从上往下执行- 加上
@pytest.mark.run(order=3)
装饰器之后,执行顺序按照按照order 值
从大往小执行 - 未加的用例先执行加了排序装饰器的,剩余按照默认顺序执行
以上为内容纯属个人理解,如有不足,欢迎各位大神指正,转载请注明出处!
如果觉得文章不错,欢迎关注微信公众号,微信公众号每天推送相关测试技术文章