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 ===========================
    

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

  • 相关阅读:
    理解区块链背后的Merkle Tree
    黑科技!当会爬虫的Python遇上会画图的FineBI……
    PlayFramework 1.x 生产环境部署 源码编译
    特别推荐:系统性能提升优先法宝 | 缓存应用实践
    Python爬虫入门教程 38-100 教育部高校名单数据爬虫 scrapy
    BI分析工具选型:从PowerBI到FineBI的9个理由!
    集群的定义以及类别定义
    python cx_oracle 绑定变量
    [故障解决]图文:windows apache无法启用 端口被占用
    设计模式之抽象工厂模式
  • 原文地址:https://www.cnblogs.com/yoyoketang/p/12798200.html
Copyright © 2011-2022 走看看