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

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

  • 相关阅读:
    推荐一本不错的书《Sencha Ext JS 5 Bootcamp in a Book》
    Libgdx 1.6.0发布,跨平台游戏开发框架
    《.NET最佳实践》与Ext JS/Touch的团队开发
    【翻译】Sencha Cmd中脚本压缩方法之比较
    【翻译】Ext JS 6早期访问版本发布
    【翻译】Ext JS 6有什么新东西?
    SpringBoot 使用 MyBatis 分页插件 PageHelper 进行分页查询
    Spring boot+Thymeleaf+easyui集成:js创建组件页面报错
    SpringBoot多模块搭建,依赖管理
    IDEA在同一窗口导入多个项目
  • 原文地址:https://www.cnblogs.com/yoyoketang/p/12798200.html
Copyright © 2011-2022 走看看