Skip to content

Commit ea22beb

Browse files
authored
Fix error with pytest --strict-markers (#300)
* refactor(common_pytest_marks.py): strictly check type for >= python3 and for earlier versions of python construct a dummy MarkDecorator * test(xfail_marker): use of default markers in strict mode
1 parent d1c8c55 commit ea22beb

File tree

5 files changed

+31
-10
lines changed

5 files changed

+31
-10
lines changed

src/pytest_cases/common_pytest_marks.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -292,29 +292,25 @@ def markinfos_to_markdecorators(marks, # type: Iterable[Mark]
292292
with warnings.catch_warnings():
293293
warnings.simplefilter("ignore")
294294
for m in marks:
295-
# create a dummy new MarkDecorator named "MarkDecorator" for reference
296-
md = pytest.mark.MarkDecorator()
297-
298295
if PYTEST3_OR_GREATER:
299-
if isinstance(m, type(md)):
296+
if isinstance(m, MarkDecorator):
300297
# already a decorator, we can use it
301298
marks_mod.append(m)
302299
else:
303-
md.mark = m
300+
md = MarkDecorator(m)
304301
marks_mod.append(md)
305302
else:
303+
# create a dummy new MarkDecorator named "MarkDecorator" for reference
304+
md = MarkDecorator()
306305
# always recreate one, type comparison does not work (all generic stuff)
307306
md.name = m.name
308-
# md.markname = m.name
307+
309308
if function_marks:
310309
md.args = m.args # a mark on a function does not include the function in the args
311310
else:
312311
md.args = m.args[:-1] # not a function: the value is in the args, remove it
313312
md.kwargs = m.kwargs
314313

315-
# markinfodecorator = getattr(pytest.mark, markinfo.name)
316-
# markinfodecorator(*markinfo.args)
317-
318314
marks_mod.append(md)
319315

320316
except Exception as e:

tests/pytest_extension/meta/raw/xfail_marker/__init__.py

Whitespace-only changes.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
--strict-markers
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# META
2+
# {'passed': 1, 'xfailed': 1, 'failed': 0}
3+
# END META
4+
import pytest
5+
from pytest_cases import parametrize_with_cases
6+
7+
def case_a():
8+
return "a"
9+
10+
@pytest.mark.xfail
11+
def case_b():
12+
raise RuntimeError("Expected to Fail")
13+
14+
@parametrize_with_cases("case", cases=".")
15+
def test_cases(case):
16+
assert case == "a"

tests/pytest_extension/meta/test_all.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from os.path import join, dirname, isdir, exists
77

88
import pytest
9+
from pytest import __version__
910
from pytest_cases.common_mini_six import string_types
1011

1112
# Make the list of all tests that we will have to execute (each in an independent pytest runner)
@@ -20,6 +21,11 @@
2021
# END META)
2122
.*""")
2223

24+
# Certain tests may use arguments that change name depending on the version
25+
# of pytest. Here we provide a lookup table to correct the argument
26+
# { <latest-pytest-arg> : { <major-version> : <major-version-arg> }}
27+
PYTEST_ARG_LOOKUP = {"--strict-markers": {"3": "--strict"}}
28+
2329

2430
@pytest.mark.parametrize('test_to_run', test_files, ids=str)
2531
def test_run_all_tests(test_to_run, testdir):
@@ -165,4 +171,6 @@ def real_prepare_config(args=None, plugins=None):
165171

166172

167173
def process_cmdargs(cmdargs):
168-
return shlex.split(cmdargs)
174+
# Some provide arguments may need to be converted to their corresponding
175+
# spelling depending on the pytest version.
176+
return [PYTEST_ARG_LOOKUP.get(x, {}).get(__version__[0], x) for x in shlex.split(cmdargs)]

0 commit comments

Comments
 (0)