python - pytest 属性错误 : 'Function' object has no attribute 'get_marker'

标签 python pytest

我的 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 - use Node.iter_markers(name=None) to iterate them.

关于python - pytest 属性错误 : 'Function' object has no attribute 'get_marker' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54254337/

相关文章:

python - 参数解析 : How to make mutually exclusive arguments optional?

python - 获取警告的回溯

python - 切片整个列表的切片赋值和直接赋值有什么区别?

python - 如何在 factory-boy 中使用 create_batch 时为属性设置序列?

python - 我可以使用基于 fixture 参数 ID 的 pytest.mark.skipif 吗?

python - 加速这个 for 循环 - python

python - `tiny_malloc_from_free_list` 使我的指针成为 `NULL` ?

python - 我可以通过 pytest 测试运行 line_profiler 吗?

python - 通过自身而不是方法参数的类的 Pytest fixture

python - 回滚事务不适用于 py.test 和 Flask