zoukankan      html  css  js  c++  java
  • pytest文档37-自定义用例顺序(pytest-ordering)

    前言

    测试用例在设计的时候,我们一般要求不要有先后顺序,用例是可以打乱了执行的,这样才能达到测试的效果.
    有些同学在写用例的时候,用例写了先后顺序, 有先后顺序后,后面还会有新的问题(如:上个用例返回数据作为下个用例传参,等等一系列的问题。。。)
    github 上有个 pytest-ordering 插件可以控制用例的执行顺序,github插件地址https://github.com/ftobia/pytest-ordering

    环境准备

    先安装依赖包

    pip install pytest-ordering

    使用案例

    先看pytest默认的执行顺序,是按 test_ording.py 文件写的用例先后顺序执行的

    # test_ording.py
    import pytest
    # 上海-悠悠
    
    
    def test_foo():
        print("用例11111111111")
        assert True
    
    
    def test_bar():
        print("用例22222222222")
        assert True
    
    
    def test_g():
        print("用例333333333333333")
        assert True
    

    运行结果

    D:demo>pytest test_ording.py -vs
    ============================= test session starts =============================
    platform win32 -- Python 3.6.0
    cachedir: .pytest_cache
    metadata: 
    plugins: ordering-0.6,
    collected 3 items
    
    test_ording.py::test_foo 用例11111111111
    PASSED
    test_ording.py::test_bar 用例22222222222
    PASSED
    test_ording.py::test_g 用例333333333333333
    PASSED
    
    ========================== 3 passed in 0.07 seconds ===========================
    

    使用 pytest-ordering 插件后改变测试用例顺序

    # test_ording.py
    import pytest
    # 上海-悠悠
    
    
    @pytest.mark.run(order=2)
    def test_foo():
        print("用例11111111111")
        assert True
    
    
    @pytest.mark.run(order=1)
    def test_bar():
        print("用例22222222222")
        assert True
    
    
    @pytest.mark.run(order=3)
    def test_g():
        print("用例333333333333333")
        assert True
    

    运行结果

    D:demo>pytest test_ording.py -vs
    ============================= test session starts =============================
    platform win32 -- Python 3.6.0
    cachedir: .pytest_cache
    metadata: 
    plugins: ordering-0.6,
    collected 3 items
    
    test_ording.py::test_bar 用例22222222222
    PASSED
    test_ording.py::test_foo 用例11111111111
    PASSED
    test_ording.py::test_g 用例333333333333333
    PASSED
    
    ========================== 3 passed in 0.04 seconds ===========================
    

    这样就是按指定的顺序执行的用例

  • 相关阅读:
    项目用到的分页功能(保存)
    跟上脚步(微信小程序)
    啊!!!贾克斯。
    Javascript闭包——懂不懂由你,反正我是懂了(作者: FrankFang 来源: 博客园)
    OpenCV4Android 之 OpenCV4Android SDK
    OpenCV4Android 之 Android 开发简介
    (-2147483648 > 0)?
    基于能量水平的无线传感器网络拓扑控制研究
    遍历文件夹及其子孙文件夹中的文件
    目标检测中背景建模方法
  • 原文地址:https://www.cnblogs.com/yoyoketang/p/12798200.html
Copyright © 2011-2022 走看看