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

相关推荐
Wokoo72 天前
Selenium 三大等待机制(四)
功能测试·selenium·测试用例
2601_967980113 天前
python pytest怎样测试代码
python·pytest·测试·文档·断言
2601_967980113 天前
python pytest如何编写测试
python·测试用例·pytest·函数·assert
2601_967980113 天前
Python pytest如何实现参数化测试
测试用例·pytest·python测试框架·参数化测试·ids参数
AI大模型-小华6 天前
Codex 自动测试怎么用?从生成测试用例到修复失败用例
git·测试用例·ai编程·软件开发·codex·chatgpt pro·codex cli
屿华AI7 天前
AUTOSAR 实战-诊断工具 | 基于 YAML 的诊断测试用例工程:轮询、断言与回归
数据挖掘·回归·测试用例
五网16 天前
软件测试用例模板怎么写:从用例设计到执行记录的全套指南
软件测试·论文阅读·全文检索·测试用例·论文笔记·用例设计·用例模板
测试界的小码农20 天前
【让 AI 生成的测试用例变成 pytest API 自动化脚本】
人工智能·测试用例·pytest
Da Da 泓25 天前
五子棋 Web 端测试用例报告
前端·网络·websocket·网络协议·测试用例·项目
会开车的鸟哥25 天前
自动创建测试用例与组件测试向导
测试用例·simuinktest