pytest_collection_modifyitems
时间: 2023-04-29 13:00:24 浏览: 67
pytest_collection_modifyitems是pytest框架中的一个钩子函数,用于修改测试用例的收集结果。在这个钩子函数中,可以对收集到的测试用例进行筛选、排序、重组等操作,以满足测试需求。该函数的返回值是一个列表,其中每个元素都是一个修改后的测试用例对象。
相关问题
翻译 pytest_collection(session)[source] Perform the collection phase for the given session. Stops at first non-None result, see firstresult: stop at first non-None result. The return value is not used, but only stops further processing. The default collection phase is this (see individual hooks for full details): Starting from session as the initial collector: pytest_collectstart(collector) report = pytest_make_collect_report(collector) pytest_exception_interact(collector, call, report) if an interactive exception occurred For each collected node: If an item, pytest_itemcollected(item) If a collector, recurse into it. pytest_collectreport(report) pytest_collection_modifyitems(session, config, items) pytest_deselected(items) for any deselected items (may be called multiple times) pytest_collection_finish(session) Set session.items to the list of collected items Set session.testscollected to the number of collected items You can implement this hook to only perform some action before collection, for example the terminal plugin uses it to start displaying the collection counter (and returns None). Parameters session (Session) – The pytest session object.
函数 pytest_collection(session) 的作用是执行给定会话的收集阶段。如果有第一个非 None 的结果,函数会停止执行,这受 firstresult 参数的控制。返回值不会被使用,但会停止进一步的处理过程。默认的收集阶段如下(请查看各个钩子的完整细节):
从 session 作为初始收集器开始:
pytest_collectstart(collector)
report = pytest_make_collect_report(collector)
pytest_exception_interact(collector, call, report) 如果发生交互式异常
对于每个收集到的节点:
如果是 item,则执行 pytest_itemcollected(item)
如果是 collector,则递归进入它
pytest_collectreport(report)
pytest_collection_modifyitems(session, config, items)
对于任何已取消选择的项目,执行 pytest_deselected(items)(可能会被多次调用)
pytest_collection_finish(session)
将 session.items 设置为收集到的项目列表
将 session.testscollected 设置为收集到的项目数量
你可以实现这个钩子来只在收集前执行某些操作,例如终端插件使用它来开始显示收集计数器(并返回 None)。
参数 session(Session):pytest 会话对象。
编写pytest钩子函数
编写pytest钩子函数时,可以使用以下几个常用的钩子函数:
1. pytest_generate_tests(metafunc):当pytest生成测试时,pytest将调用此函数。它可以帮助你动态生成测试。
2. pytest_collection_modifyitems(config, items):在收集测试用例之后,可以使用此钩子函数修改测试用例的集合。可以用于筛选、排序或修改测试用例集合。
3. pytest_configure(config):在pytest的配置阶段,可以使用此钩子函数执行一些初始化操作,例如加载配置文件、注册插件等。
4. pytest_unconfigure(config):在pytest的配置阶段结束时,可以使用此钩子函数执行一些清理操作,例如关闭数据库连接、释放资源等。
5. pytest_runtest_protocol(item, nextitem):在每个测试用例执行之前,可以使用此钩子函数执行一些准备工作,例如设置测试环境、准备测试数据等。
6. pytest_report_header(config):在生成测试报告的头部,可以使用此钩子函数添加自定义的报告信息。
7. pytest_terminal_summary(terminalreporter):在测试执行完成后,可以使用此钩子函数执行一些后处理操作,例如生成报告、发送邮件等。