使用 python 获取所有 pytest 节点 ID 的列表 [英] Get a list of all pytest node ids using python

查看:115
本文介绍了使用 python 获取所有 pytest 节点 ID 的列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您知道是否有办法收集所有 pytest 节点 ID(如此处) 使用 pytest python API ?

Do you know if there is a way to collect all pytest node ids (as presented here) using the pytest python API ?

我找到了pytest的--collect-only参数,但是我不知道如何使用python获取输出?

I have found the --collect-only parameter of pytest, but I can't figure out how to get the output using python ?

提前致谢!

推荐答案

如果您想以编程方式访问 nodeid,最好编写一个小插件,将它们存储在测试集合中.示例:

If you want to access nodeids programmatically, best is to write a small plugin that will store them on test collection. Example:

import pytest


class NodeidsCollector:
    def pytest_collection_modifyitems(self, items):
        self.nodeids = [item.nodeid for item in items]


def main():
    collector = NodeidsCollector()
    pytest.main(['--collect-only'], plugins=[collector])
    # use collector.nodeids now

如果你想避免 pytest 输出到终端,另外禁用终端插件:

If you want to avoid pytest output to the terminal, disable the terminal plugin in addition:

pytest.main(['--collect-only', '-pno:terminal'], plugins=[collector])

这篇关于使用 python 获取所有 pytest 节点 ID 的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆