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

相关推荐
2601_962077605 小时前
从零搭建Python接口自动化测试框架:pytest+requests实战指南
python·pytest·接口自动化·requests·框架搭建
weixin_440730507 小时前
pytest结合allure生成html测试报告(step、story、severity、screenshot)
前端·html·pytest·allure报告
拜托多多指教8 小时前
【Pytest框架学习】分层架构
python·pytest
2601_962299888 小时前
python脚本如何单元测试
python·单元测试·pytest·unittest·mock对象
测试199819 小时前
如何用appium搭建Android自动化测试框架?
自动化测试·软件测试·python·测试工具·职场和发展·appium·测试用例
程序员杰哥1 天前
如何做接口测试?
自动化测试·python·测试工具·职场和发展·测试用例·接口测试·postman
Tizzy JJ1 天前
Python + pytest 接口自动化测试框架实战:从零搭建企业级项目骨架
开发语言·python·pytest
Tizzy JJ1 天前
接口自动化测试实战:如何围绕真实业务场景设计高质量用例
python·自动化·pytest
2601_962382432 天前
系统学习Python——单元测试unittest:执行测试用例(unit test python)
python·测试用例·测试框架·unittest·测试集合