Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pytest AttributeError: 'Function' object has no attribute 'get_marker'

Tags:

python

pytest

I have this code in my conftest.py:

def pytest_collection_modifyitems(config, items):
    items.sort(key=lambda x: 2 if x.get_marker('slow') else 1)

Lately it started to cause these exceptions:

$ 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 ================================================
like image 502
Messa Avatar asked Jan 18 '19 12:01

Messa


1 Answers

Pytest has changed its API in version 4.

Quick solution: use get_closest_marker() instead of get_marker():

def pytest_collection_modifyitems(config, items):
    items.sort(key=lambda x: 2 if x.get_closest_marker('slow') else 1)

See 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.

like image 185
Messa Avatar answered Oct 26 '22 11:10

Messa