Pytest插件pytest-order指定用例顺序

pytest,插件,order,指定,顺序 · 浏览次数 : 585

小编点评

**pytest-order**是一个用于指定用例顺序的插件,它是一个 fork of `pytest-ordering`,它提供一些额外的功能,例如顺序中的用例与其他测试用例之间的依赖关系。 **使用方法:** 1. 安装 `pytest-order`:```pip install pytest-order``` 2. 在测试用例中使用 `pytest.mark.order()`装饰器来指定用例顺序。 3. 使用 `@pytest.mark.order()` 装饰器指定用例顺序的优先级。 **示例:** ```python import pytest @pytest.mark.order(index=2) def test_three(): print('three') assert 3 == 3 @pytest.mark.order('second') def test_two(): print('two') assert 2 == 2 @pytest.mark.order(-1) def test_last(): print('last') assert 'last' == 'last' @pytest.mark.order(0) def test_one(): print('one') assert 1 == 1 # 测试其他用例 ``` **注意:** * `pytest-order` 现在已不再维护,建议使用 `pytest-ordering` 或其他替代插件。 * 在使用 `pytest-order` 时,请确保测试用例的顺序正确。 * `pytest-order` 的用法类似于 `pytest-ordering`,但它提供了额外的功能。

正文

Pytest插件pytest-order指定用例顺序

安装

 pip install pytest-order
  • 注意不是pytest-ordering

  • 说起来这里有个故事

 

关于pytest-ordering和pytest-order

https://github.com/ftobia/pytest-ordering

https://github.com/pytest-dev/pytest-order

  • 在写这个文章之前,我用的一直是pytest-ordering

  • 但我在安装pytest-ordering的时候一直看到有pytest-order

  • 今天特意看了下,好家伙

    • GITHUB上写了这么一句:pytest-ordering is no longer maintained, please use https://pypi.org/project/pytest-order/

    • pytest-order is a fork of pytest-ordering that provides additional features like ordering relative to other tests.

    • pytest-order works with Python 3.6 - 3.10, with pytest versions >= 5.0.0 for all versions except Python 3.10, and for pytest >= 6.2.4 for Python 3.10. pytest-order runs on Linux, macOS and Windows.

  • 所以pytest-ordering在当前的pytest及python版本中可能会出现问题,而pytest-order是同步更新的,你可以放心食用

  • 那么问题来了,pytest-order怎么用呢?看官方的示例跟pytest-ordering还是有区别的(如果没有区别,装了2个的话你可能会分不清哪个调用的,当然我们不推荐你用2个)

    • 虽然用起来很简单,但的确蛮细节的,网上你看到的基本都是pytest-ordering的用法,然它在3年前就停止更新了

    • 我把它的git@github.com:pytest-dev/pytest-order.git,下下来,它写了很多的example,嗯~

     

根据索引排序

  • 其实就一个数字

  • 也可以是与之对应的特定字符,如first等

    字符数字index
    first 0
    second 1
    last -1
    second_to_last -2
    eighth_to_last -8
  • 示例1

     import pytest
     
     
     @pytest.mark.order(index=2)
     def test_three():
         print('three')
         assert 3 == 3
     
     
     @pytest.mark.order('second')
     def test_two():
         print('two')
         assert 2 == 2
     
     @pytest.mark.order(-1)
     def test_last():
         print('last')
         assert 'last' == 'last'
     
     @pytest.mark.order(0)
     def test_one():
         print('one')
         assert 1 == 1
     
     
     if __name__ == '__main__':
         pytest.main(['-sv', __file__])
     
     test_order_v1.py::test_one one
     PASSED
     test_order_v1.py::test_two two
     PASSED
     test_order_v1.py::test_three three
     PASSED
     test_order_v1.py::test_last last
     PASSED
     
     ============================== 4 passed in 0.06s ==============================
     
     进程已结束,退出代码为 0
     
  • index是从0开始的;切记index=2其实是第三个,first其实是0.所以我们不建议混用

  • -1是最后一个没有问题,索引体系跟list的类似,还是比较好理解的。建议用数字,学习成本就比较低。

  • 这个装饰器可以用到类上

     import pytest
     
     
     @pytest.mark.order(2)
     class TestA:
         def test_one(self):
             assert 1 == 1
         def test_two(self):
             assert 1 == 1
     
     @pytest.mark.order(1)
     class TestB:
         def test_one(self):
             assert 1 == 1
         def test_two(self):
             assert 1 == 1
     
     
     if __name__ == '__main__':
         pytest.main(['-sv', __file__])
     
     test_order_v1.py::TestB::test_one PASSED
     test_order_v1.py::TestB::test_two PASSED
     test_order_v1.py::TestA::test_one PASSED
     test_order_v1.py::TestA::test_two PASSED

     

 

排在指定用例后面

  • 直接看示例

     import pytest
     
     
     @pytest.mark.order(after='test_hallo')
     def test_hello():
         assert 1 == 1
     
     
     def test_hallo():
         assert 1 == 1
     
     
     if __name__ == '__main__':
         pytest.main(['-sv', __file__])
     
  • 还可以这样写,指定类

     @pytest.mark.order(after="TestB::test_c")
     @pytest.mark.order(after="Test2")
     
  • 可以指定文件夹/文件::类::测试用例(即测试函数名),也可以用before

     @pytest.mark.order(before="test_module_c/test_submodule.py::test_2")
     

     

  • 组合也可以

     @pytest.mark.order(index=0, after="test_second")
     @pytest.mark.order(after=["test_second", "other_module.py::test_other"])
     
  • 如果是参数化,那就直接用测试函数名

     import pytest
     
     @pytest.mark.order(after=["test_second"])
     def test_first():
         assert True
     
     @pytest.parametrize(param, [1, 2, 3])
     def test_second(param):
         assert True

     

说在最后

  • 关于用例的顺序相关的插件是不少的,比如

    • pytest-randomly:随机顺序

    • pytest-reverse:反转(通过一个hook亦可实现)

    • pytest-random-order :随机顺序

    • pytest-depends:依赖

    • pytest-find-dependencies:寻找依赖

  • 写完发现他有个doc,白整了~

     https://pytest-order.readthedocs.io/en/latest/

     

  •  

与Pytest插件pytest-order指定用例顺序相似的内容:

Pytest插件pytest-order指定用例顺序

Pytest插件pytest-order指定用例顺序 安装 pip install pytest-order 注意不是pytest-ordering 说起来这里有个故事 关于pytest-ordering和pytest-order https://github.com/ftobia/pytest-o

Python中的枚举类enum

0. 本文来历 上一篇文章,我写了Pytest插件pytest-order指定用例顺序 我当时就比较好奇它的顺序和英文的对应关系,肯定是写死的,找了下就发现在源码sorter.py中定义了一个dict如下 orders_map = { "first": 0, "second": 1, "third"

Pytest插件pytest-assume多重断言

Pytest插件pytest-assume多重断言 背景 import pytest def test_assume1(): assert 1 == 2 print('hello') assert 2 == 3 if __name__ == '__main__': pytest.main(['-sv

Pytest插件pytest-repeat重复执行

Pytest插件pytest-repeat重复执行 安装 pip install pytest-repeat doc https://pypi.org/project/pytest-repeat/ https://github.com/pytest-dev/pytest-repeat 2020年10

Pytest插件pytest-rerunfailures失败重跑

Pytest插件pytest-rerunfailures失败重跑 安装 pip install pytest-rerunfailures doc https://github.com/pytest-dev/pytest-rerunfailures https://pypi.org/project/p

浅谈Pytest中的marker

浅谈Pytest中的marker 没有注册marker 我们写一个简单的测试 # test_demo.py import pytest @pytest.mark.login def test_demo(): assert True 你运行的话会有如下提示 test_demo.py:4: Pytest

浅谈Pytest中的warning处理

浅谈Pytest中的warning处理 没有处理warning 我们写一个简单的测试 import pytest def test_demo(): import warnings warnings.warn('test warn',DeprecationWarning) assert True if

详谈pytest中的xfail

详谈pytest中的xfail 原文链接: https://docs.pytest.org/en/7.2.x/how-to/skipping.html 链接中详细阐述了skip和xfail两种情况 xfail 应该译作expected fail,预期失败(我知道这个用例是因为某些原因会失败的) 有哪

数据驱动测试-从方法探研到最佳实践

作者:刘红妍 导读 在自动化测试实践中,测试数据是制造测试场景的必要条件,本文主要讲述了在沟通自动化框架如何分层,数据如何存储,以及基于单元测试pytest下如何执行。并通过实践案例分享,提供数据驱动测试的具体落地方案。 基本概念 数据驱动测试(DDT)是一种方法,其中在数据源的帮助下重复执行相同顺

pytest7.4版本的一个变更,可能会影响你的项目

# pytest7.4版本的一个变更,可能会影响你的项目 > 本文撰写于 2023.7.10 # 准备工作 - 项目结构如下 ``` D:\Gitee\DemoRepo (17.97MB) +-- testCases (1.03KB) | +-- conftest.py (252b) | +-- p