Pytest 测试用例执行顺序自定义 Pytest-ordering

一. 简介

Pytest-ordering 是一个 Pytest 插件,可自定义 Pytest 测试用例的执行顺序。这对于确保特定的测试用例在其他测试用例之前或之后运行非常有用。


二. 使用场景

对于集成测试,经常会有上下文依赖关系的测试用例。比如 10 个步骤,拆成 10 条 case,这时候能知道到底执行到哪步报错。而用例默认执行顺序是自上而下执行的。这时可以通过 setup,teardown 和 fixture 来解决。也可以使用对应的插件。


三. 使用步骤

  • 安装pip install pytest-ordering

  • 用法@pytest.mark.run(order=2)

    • 注意:多个插件装饰器(>2)的时候,有可能会发生冲突。

      • 代码示例:不使用 Pytest-ordering

        python 复制代码
        import pytest
        
        #@pytest.mark.run(order=2)
        @pytest.mark.third
        def test_foo():
            assert True
        
        # @pytest.mark.run(order=1)
        @pytest.mark.first
        def test_bar():
            assert True
        
        # @pytest.mark.run(order=2)
        @pytest.mark.second
        def test_bar1():
            assert True
        • 运行结果:可以看出是按照顺执行的测试用例
      • 代码示例:使用 Pytest-ordering

        python 复制代码
        import pytest
        
        @pytest.mark.run(order=2)
        @pytest.mark.third
        def test_foo():
            assert True
        
        @pytest.mark.run(order=1)
        @pytest.mark.first
        def test_bar():
            assert True
        
        @pytest.mark.run(order=2)
        @pytest.mark.second
        def test_bar1():
            assert True
        • 再次运行,结果为1、test_bar,2、test_bar1,3、test_foo

相关推荐
程序员小远16 天前
自动化测试基础知识总结
自动化测试·软件测试·python·selenium·测试工具·职场和发展·测试用例
程序员三藏17 天前
Web自动化测试详解
自动化测试·软件测试·python·selenium·测试工具·职场和发展·测试用例
糖果店的幽灵17 天前
软件测试接口测试从入门到精通:Python接口自动化 - pytest测试框架
软件测试·python·功能测试·自动化·pytest·接口测试
oscar99917 天前
AI 测试用例生成:在 Katalon True Platform 中从需求直达执行
人工智能·测试用例·katalon
2601_9618752418 天前
花生十三资料1200题|题库|刷题
conda·pytest·pillow·pip·web3.py·ipython·gunicorn
某人辛木18 天前
Web自动化测试
前端·python·pycharm·pytest
淡漠的蓝精灵20 天前
pytest-xdist:把 pytest 测试分发到多核 CPU 执行
其他·pytest
weixin_3077791321 天前
从脚本执行到智能体协作:AI辅助测试能力的范式重构
运维·开发语言·人工智能·算法·测试用例
jjjava2.022 天前
软件测试与开发全流程解析
java·功能测试·测试用例