一、前言

在pytest中,测试用例的默认执行顺序是从上到下执行的,但是有时候我们会有这样的需求,就是打乱测试用例的执行顺序来达到某个测试效果,这时候就需要用到Pytest中的一个插件,pytest-ordering是专门用来调整用例执行顺序的。

二、学习目标

1.pytest-ordering安装

2.pytest-ordering应用

三、知识点

1.【pytest-ordering安装】

既然pytest-ordering是pytest的一个插件,插件就需要安装,插件其实就是一个python的三方模块。

pip install pytest-ordering

2.【pytest-ordering应用】

  • 语法:

    @pytest.mark.run(order)  #order序号
    
  • 代码示例:

    import pytest
    
    @pytest.mark.run(order = 2)
    def test_01():
        print("---用例1执行---")
    
    @pytest.mark.run(order = 1)
    class TestCase():
    
        def test_02(self):
            print("---用例2执行---")
    
        def test_03(self):
            print("---用例3执行---")
    
  • 运行效果:

    test_demo.py::TestCase::test_02 PASSED                                   [ 33%]---用例2执行---
    
    test_demo.py::TestCase::test_03 PASSED                                   [ 66%]---用例3执行---
    
    test_demo.py::test_01 PASSED                                             [100%]---用例1执行---
    ============================== 3 passed in 0.02s ==============================