Python pytest AttributeError:';功能';对象没有属性';获取标记';

Python pytest AttributeError:';功能';对象没有属性';获取标记';,python,pytest,Python,Pytest,我的conftest.py中有此代码: def pytest\u collection\u modifyitems(配置,项): items.sort(key=lambda x:2如果x.get\u标记('slow'),则为1) 最近,它开始导致以下异常: $ venv/bin/py.test -vv --tb=short tests ============================================================================ te

我的
conftest.py中有此代码:

def pytest\u collection\u modifyitems(配置,项):
items.sort(key=lambda x:2如果x.get\u标记('slow'),则为1)
最近,它开始导致以下异常:

$ venv/bin/py.test  -vv --tb=short tests
============================================================================ test session starts ============================================================================
platform darwin -- Python 3.5.6, pytest-4.1.1, py-1.7.0, pluggy-0.8.1 -- /Users/.../venv/bin/python3.5
cachedir: .pytest_cache
rootdir: /Users/..., inifile:
collecting ... INTERNALERROR> Traceback (most recent call last):
INTERNALERROR>   File "/Users/.../venv/lib/python3.5/site-packages/_pytest/main.py", line 203, in wrap_session
...
INTERNALERROR>   File "/Users/.../venv/lib/python3.5/site-packages/pluggy/callers.py", line 187, in _multicall
INTERNALERROR>     res = hook_impl.function(*args)
INTERNALERROR>   File "/Users/.../tests/conftest.py", line 14, in pytest_collection_modifyitems
INTERNALERROR>     items.sort(key=lambda x: 2 if x.get_marker('slow') else 1)
INTERNALERROR>   File "/Users/.../tests/conftest.py", line 14, in <lambda>
INTERNALERROR>     items.sort(key=lambda x: 2 if x.get_marker('slow') else 1)
INTERNALERROR> AttributeError: 'Function' object has no attribute 'get_marker'

======================================================================= no tests ran in 0.30 seconds ================================================
$venv/bin/py.test-vv--tb=短测试
===========================================================================================================================测试会话开始============================================================================
平台darwin——Python 3.5.6、pytest-4.1.1、py-1.7.0、Plugy-0.8.1--/Users/../venv/bin/python3.5
cachedir:.pytest\u缓存
rootdir:/Users/…,文件:
收集。。。INTERNALERROR>回溯(最近一次呼叫上次):
内部错误>文件“/Users/../venv/lib/python3.5/site packages/\u pytest/main.py”,第203行,在wrap\u会话中
...
INTERNALERROR>File“/Users/../venv/lib/python3.5/site packages/pluggy/callers.py”,第187行,在多调用中
INTERNALERROR>res=hook_impl.function(*args)
内部错误>文件“/Users/../tests/conftest.py”,第14行,在pytest\u集合\u modifyitems中
INTERNALERROR>items.sort(key=lambda x:2如果x.get\u标记('slow'),则为1)
内部错误>文件“/Users/../tests/conftest.py”,第14行,在
INTERNALERROR>items.sort(key=lambda x:2如果x.get\u标记('slow'),则为1)
INTERNALERROR>AttributeError:“函数”对象没有属性“get\u marker”
============================================================================================================================0.30秒内没有运行任何测试================================================

Pytest在版本4中更改了其API

快速解决方案:使用
获取最近的标记()
而不是
获取标记()

def pytest\u collection\u modifyitems(配置,项):
items.sort(key=lambda x:2如果x.get\u最近的\u标记('slow'),否则为1)

删除
节点。获取标记(名称)
返回值不能用于多个存在性检查

使用
节点。获取\u最近的\u标记(名称)
作为替换

删除
testfunction.markername
属性-使用
Node.iter\u标记(name=None)
迭代它们


使用
pytest版本4.6.3
在一个非常简单的测试中发现此错误。在搜索过程中,我发现了下面的建议并实施了它

pip安装pytest==3.10.1

它马上就起作用了。
我认为这应该是@Messa-his答案中提到的更改的结果。

最新文档建议使用item.iter\u标记

问题链接它是
pytest 4.1.0+
pytest cov 2.6.0
# content of conftest.py
import sys


def pytest_runtest_setup(item):
    for mark in item.iter_markers(name="glob"):
        print("glob args={} kwargs={}".format(mark.args, mark.kwargs))
        sys.stdout.flush()