Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 仅当使用了--collect only时才运行方法_Python_Pytest - Fatal编程技术网

Python 仅当使用了--collect only时才运行方法

Python 仅当使用了--collect only时才运行方法,python,pytest,Python,Pytest,我试图收集每个特性或团队有多少测试用例的一些统计数据,并且只想在收集过程中这样做,而不是当我公司的每个人都运行他们的测试时。我正在寻找一种仅当py.test命令中使用了--collect only时才运行函数的方法 目标: 仅当使用了--collect only时才运行一些代码 我需要进入办公室 测试项目数据结构(即,我需要所有 (已使用) 目前,我通过pytest\u collection\u modifyitemshook: def pytest_collection_modifyite

我试图收集每个特性或团队有多少测试用例的一些统计数据,并且只想在收集过程中这样做,而不是当我公司的每个人都运行他们的测试时。我正在寻找一种仅当
py.test
命令中使用了
--collect only
时才运行函数的方法

目标

  • 仅当使用了
    --collect only
    时才运行一些代码
  • 我需要进入办公室 测试
    项目
    数据结构(即,我需要所有 (已使用)
目前,我通过
pytest\u collection\u modifyitems
hook:

def pytest_collection_modifyitems(config, items):
    # This hook runs after collection
    for item in items:
        teams = [mark.name for mark in item.iter_markers() if mark.name.startswith('team_')]
        features = [mark.name for mark in item.iter_markers() if mark.name.startswith('feature_')]

        # do something with these markers
当且仅当py.test与
--collect only
一起运行时,是否有方法运行上述代码?如果有人对此有更好的建议,请帮助


谢谢

只需检查是否在
config
对象中设置了该标志。hookimpl的示例:

def pytest_collection_modifyitems(config, items):
    if config.option.collectonly:
        print("I will run only with --collectonly flag")
    else:
        print("I will run only without --collectonly flag")

谢谢你,这正是我们最终要做的!问这个问题时,我不知道
config.option
可以访问所有的输入参数。谢谢