我的 conftest.py
中有此代码:
def pytest_collection_modifyitems(config, items):
items.sort(key=lambda x: 2 if x.get_marker('slow') else 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 ================================================
最佳答案
Pytest 在第 4 版中更改了其 API。
快速解决方案:使用 get_closest_marker()
而不是 get_marker()
:
def pytest_collection_modifyitems(config, items):
items.sort(key=lambda x: 2 if x.get_closest_marker('slow') else 1)
见 https://github.com/pytest-dev/pytest/pull/4564
Remove
Node.get_marker(name)
the return value was not usable for more than a existence check.Use
Node.get_closest_marker(name)
as a replacement.Remove
testfunction.markername
attributes - useNode.iter_markers(name=None)
to iterate them.
关于python - pytest 属性错误 : 'Function' object has no attribute 'get_marker' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54254337/