zoukankan      html  css  js  c++  java
  • pytest文档34-Hooks函数改变用例执行顺序(pytest_collection_modifyitems)

    前言

    有一些小伙伴一直想改变pytest用例的执行顺序,实际上我们在用例设计原则上用例就不要有依赖顺序。
    pytest默认执行用例是先根据项目下的文件夹名称按ascii码去收集的,module里面的用例是从上往下执行的.
    pytest_collection_modifyitems 这个钩子函数顾名思义就是改变用例的执行顺序。

    pytest_collection_modifyitems

    pytest_collection_modifyitems 功能是当测试用例收集完成后,可以改变测试用例集合(items)的顺序

    def pytest_collection_modifyitems(session, config,items):
        '''called after collection is completed. 
        you can modify the ``items`` list
        :param _pytest.main.Session session: the pytest session object
        :param _pytest.config.Config config: pytest config object
        :param List[_pytest.nodes.Item] items: list of item objects
        '''
    

    items是用例对象的一个列表,改变items里面用例的顺序就可以改变用例的执行顺序了。

    pytest默认执行顺序

    先设计一个简单的 pytest 项目,有a和b两个包,分别在 test_a.py 和 test_b.py 写测试用例

    conftest.py内容

    import pytest
    # 上海-悠悠
    
    def pytest_collection_modifyitems(session, items):
        print("收集到的测试用例:%s"%items)
    

    test_a.py内容

    def test_a_1():
        print("测试用例a_1")
    
    def test_a_2():
        print("测试用例a_2")
    

    test_b.py内容

    def test_b_2():
        print("测试用例b_2")
    
    def test_b_1():
        print("测试用例b_1")
    

    运行完成后可以看到收集到的测试用例,会在测试用例开始执行执行

    D:demo2>pytest
    ============================= test session starts =============================
    platform win32 -- Python 3.6.0, pytest-4.5.0, py-1.5.4, pluggy-0.13.1
    rootdir: D:softcodepytest_jenkins_demodemo2
    plugins: allure-pytest-2.8.6
    collecting ... 收集到的测试用例:[<Function test_a_1>, <Function test_a_2>, <Function test_b_2>, <Function test_b_1>]
    collected 4 items
    
    a	est_aaa.py ..                                                         [ 50%]
    b	est_bbb.py ..                                                         [100%]
    
    ========================== 4 passed in 0.06 seconds ===========================
    

    从结果可以看出运行的时候先按模块名称ascii码去收集,单个py文件里面的用例按从上到下写的顺序收集。

    items用例排序

    如果我想改变上面的用例执行顺序,以用例名称ascii码排序。先获取到用例的名称,以用例名称排序就可以了。

    # 上海-悠悠
    def pytest_collection_modifyitems(session, items):
        print(type(items))
        print("收集到的测试用例:%s" % items)
        # sort排序,根据用例名称item.name 排序
        items.sort(key=lambda x: x.name)
        print("排序后的用例:%s" % items)
        for item in items:
            print("用例名:%s" % item.name)
    

    重新执行后结果

    
    D:softcodepytest_jenkins_demodemo2>pytest -s
    ============================= test session starts =============================
    platform win32 -- Python 3.6.0, pytest-4.5.0, py-1.5.4, pluggy-0.13.1
    rootdir: D:softcodepytest_jenkins_demodemo2
    plugins:
    collecting ... <class 'list'>
    收集到的测试用例:[<Function test_a_1>, <Function test_a_2>, <Function test_b_2>, <Function test_b_1>]
    排序后的用例:[<Function test_a_1>, <Function test_a_2>, <Function test_b_1>, <Function test_b_2>]
    用例名:test_a_1
    用例名:test_a_2
    用例名:test_b_1
    用例名:test_b_2
    collected 4 items
    
    a	est_aaa.py 测试用例a_1
    .测试用例a_2
    .
    b	est_bbb.py 测试用例b_1
    .测试用例b_2
    .
    
    ========================== 4 passed in 0.06 seconds ===========================
    

    重新排序后就可以按用例的名称顺序执行了。

    有个 pytest-ordering 插件也可以自定义用例的顺序https://github.com/ftobia/pytest-ordering

  • 相关阅读:
    【故障处理】ORA-12162: TNS:net service name is incorrectly specified (转)
    android studio 编程中用到的快捷键
    java时间格式串
    android Error occurred during initialization of VM Could not reserve enough space for object heap Could not create the Java virtual machine.
    linux安装vmware
    x1c 2017 安装mint18的坑——grub2
    x1c2017 8G版 win linux的取舍纠结记录
    python的try finally (还真不简单)
    kafka+docker+python
    json文件不能有注释
  • 原文地址:https://www.cnblogs.com/yoyoketang/p/12624000.html
Copyright © 2011-2022 走看看