【pytest】Hook 方法之 pytest_collection_modifyitems:修改测试用例执行顺序

Hook 方法之 pytest_collection_modifyitems:

pytest_collection_modifyitems 是在用例收集完毕之后被调用,可以用来调整测试用例执行顺序;
它有三个参数,分别是:

  1. session:会话对象;
  2. config:配置对象;
  3. items:用例对象列表;

这三个参数分别有不同的作用,都可以拿来单独使用,修改用例执行顺序主要是使用 items 参数!

def pytest_collection_modifyitems(session, config, items):
    """ called after collection has been performed, may filter or re-order
    the items in-place.

    :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
    """

 修改测试用例执行顺序:

首先准备三个测试用例:

import pytest

class TestDemoA:

    def test_A_001(self):
        pass

    def test_A_002(self):
        pass

    def test_A_003(self):
        pass

if __name__ == '__main__':
    pytest.main(['-s'])

 普通情况下pytest 会按照从上到下的顺序依次执行(模块级会先以模块名按ascii编码进行排序):

# 控制台输出结果:
============================= test session starts =============================
collected 3 items
test_Z.py::TestDemoA::test_A_001 PASSED
test_Z.py::TestDemoA::test_A_002 PASSED
test_Z.py::TestDemoA::test_A_003 PASSED
============================== 3 passed in 0.03s ==============================

使用 pytest_collection_modifyitems :

在 conftest.py 文件中 使用 pytest_collection_modifyitems 钩子方法:

# conftest.py

# 在收集完测试用例后才会执行
def pytest_collection_modifyitems(items):
    print('pytest 收集到的所有测试用例:\n',items)

if __name__ == '__main__':
    pytest.main(['-s'])

 我们可以看到控制台中打印出来了收集到的三个测试用例的对象,而且是在测试用例收集之前便已经执行,在执行完 pytest_collection_modifyitems 之后才显示收集到了 3 个用例,如果我们在 pytest_collection_modifyitems 中对用例进行调整,便会影响用例是否执行和执行顺序;

# 控制台输出结果:
============================= test session starts =============================

pytest 收集到的所有测试用例:
 [<Function test_A_001>, <Function test_A_002>, <Function test_A_003>]

collected 3 items
test_Z.py::TestDemoA::test_A_001 PASSED
test_Z.py::TestDemoA::test_A_002 PASSED
test_Z.py::TestDemoA::test_A_003 PASSED

============================== 3 passed in 0.02s ==============================

利用 items 获取收集到的用例名和用例节点:

# conftest.py

def pytest_collection_modifyitems(items):
    print('pytest 收集到的所有测试用例:\n', items)
    for item in items:
        print('---' * 10)
        print('用例名:', item.name)
        print('用例节点:', item.nodeid)

if __name__ == '__main__':
    pytest.main(['-s'])
# 控制台输出结果:
============================= test session starts =============================

pytest 收集到的所有测试用例:
 [<Function test_A_001>, <Function test_A_002>, <Function test_A_003>]
------------------------------
用例名: test_A_001
用例节点: pytestDemo/pytest_hook_demo/test_Z.py::TestDemoA::test_A_001
------------------------------
用例名: test_A_002
用例节点: pytestDemo/pytest_hook_demo/test_Z.py::TestDemoA::test_A_002
------------------------------
用例名: test_A_003
用例节点: pytestDemo/pytest_hook_demo/test_Z.py::TestDemoA::test_A_003

collected 3 items
test_Z.py::TestDemoA::test_A_001 PASSED
test_Z.py::TestDemoA::test_A_002 PASSED
test_Z.py::TestDemoA::test_A_003 PASSED

============================== 3 passed in 0.02s ==============================

修改用例执行顺序和剔除测试用例:

# conftest.py

def pytest_collection_modifyitems(items):
    # 将用例名拿出来存入新列表
    new_items = []
    for item in items:
        new_items.append(item.name)

    # 1. 删除 test_A_002 用例
    # 获取 test_A_002 在新列表的索引
    index_2 = new_items.index('test_A_002')
    # 在老列表中删除这个索引
    del items[index_2]
    del new_items[index_2]  # 新列表同步删除,和老列表保持同步

    # 2. 调换 1 和 3 的顺序
    # 获取 1 和 3 在新列表的索引
    index_1 = new_items.index('test_A_001')
    index_3 = new_items.index('test_A_003')
    # 根据索引在老列表中调换位置
    items[index_1], items[index_3] = items[index_3], items[index_1]

 可以看到控制台输出的结果中,用例3 和 用例1 的顺序调换了,用例2 被删除没有执行;

代码写的比较粗糙,但是思路就是这样:想办法干涉 items 中用例对象的排序;

# 控制台输出结果:
============================= test session starts =============================
collected 3 items
test_Z.py::TestDemoA::test_A_003 PASSED
test_Z.py::TestDemoA::test_A_001 PASSED

============================== 2 passed in 0.02s ==============================

 

  • 4
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值