Skip to content

Commit

Permalink
test: fix pytest 4 markers usage
Browse files Browse the repository at this point in the history
A getattr on the function doesn't work since pytest 4.
Looks the correct way to do this is to test the presence of "pytestmark"
attribute on the function which is either undefined or contains a list
of Marker objects.
We don't care about backward compat with older pytest versions for
testinfra own tests.
  • Loading branch information
philpep committed Jan 14, 2019
1 parent d8e3648 commit 2b3eade
Showing 1 changed file with 8 additions and 13 deletions.
21 changes: 8 additions & 13 deletions test/conftest.py
Expand Up @@ -158,8 +158,10 @@ def host(request, tmpdir_factory):
image, kw = parse_hostspec(request.param)
spec = BaseBackend.parse_hostspec(image)

if getattr(request.function, "destructive", None) is not None:
scope = "function"
for marker in getattr(request.function, 'pytestmark', []):
if marker.name == 'destructive':
scope = "function"
break
else:
scope = "session"

Expand Down Expand Up @@ -234,20 +236,13 @@ def docker_image(host):

def pytest_generate_tests(metafunc):
if "host" in metafunc.fixturenames:
# Supported in pytest 3.6+. Earlier versions need to use the
# MarkInfo object directly as in the else: clause; this is
# marked for removal in pytest4.
if hasattr(metafunc, 'definition'):
marker = metafunc.definition.get_closest_marker(
"testinfra_hosts")
else:
marker = getattr(metafunc.function, "testinfra_hosts", None)
if marker is not None:
hosts = marker.args
for marker in getattr(metafunc.function, 'pytestmark', []):
if marker.name == 'testinfra_hosts':
hosts = marker.args
break
else:
# Default
hosts = ["docker://debian_stretch"]

metafunc.parametrize("host", hosts, indirect=True,
scope="function")

Expand Down

0 comments on commit 2b3eade

Please sign in to comment.