refactor mark tests, widen catching and make error msg more concise

This commit is contained in:
symonk 2020-04-13 14:25:01 +01:00
parent 87edc09dea
commit 251e8f212e
2 changed files with 6 additions and 47 deletions

View File

@ -84,13 +84,8 @@ def matchmark(colitem, markexpr):
"""Tries to match on any marker names, attached to the given colitem."""
try:
return eval(markexpr, {}, MarkMapping.from_item(colitem))
except (SyntaxError, TypeError):
raise UsageError(
"Marker expression provided to -m:{} was not valid python syntax."
" Please check the syntax provided and ensure it is correct".format(
markexpr
)
)
except Exception:
raise UsageError("Wrong expression passed to '-m': {}".format(markexpr))
def matchkeyword(colitem, keywordexpr):

View File

@ -601,21 +601,6 @@ class TestFunctional:
deselected_tests = dlist[0].items
assert len(deselected_tests) == 2
def test_invalid_m_option(self, testdir):
testdir.makepyfile(
"""
def test_a():
pass
"""
)
expected = (
"ERROR: Marker expression provided to -m: bogus/ was not valid python syntax. Please "
"check the syntax provided and ensure it is correct"
)
result = testdir.runpytest("-m bogus/")
result.stderr.fnmatch_lines([expected])
assert result.ret == ExitCode.USAGE_ERROR
def test_keywords_at_node_level(self, testdir):
testdir.makepyfile(
"""
@ -1027,7 +1012,8 @@ def test_pytest_param_id_allows_none_or_string(s):
assert pytest.param(id=s)
def test_ux_eval_syntax_error(testdir):
@pytest.mark.parametrize("expr", ("NOT internal_err", "NOT (internal_err)", "bogus/"))
def test_marker_expr_eval_failure_handling(testdir, expr):
foo = testdir.makepyfile(
"""
import pytest
@ -1037,29 +1023,7 @@ def test_ux_eval_syntax_error(testdir):
pass
"""
)
expected = (
"ERROR: Marker expression provided to -m: NOT internal_err was not valid python syntax. Please "
"check the syntax provided and ensure it is correct"
)
result = testdir.runpytest(foo, "-m NOT internal_err")
result.stderr.fnmatch_lines([expected])
assert result.ret == ExitCode.USAGE_ERROR
def test_ux_eval_type_error(testdir):
foo = testdir.makepyfile(
"""
import pytest
@pytest.mark.internal_err
def test_foo():
pass
"""
)
expected = (
"ERROR: Marker expression provided to -m: NOT (internal_err) was not valid python syntax. Please "
"check the syntax provided and ensure it is correct"
)
result = testdir.runpytest(foo, "-m NOT (internal_err)")
expected = "ERROR: Wrong expression passed to '-m': {}".format(expr)
result = testdir.runpytest(foo, "-m", expr)
result.stderr.fnmatch_lines([expected])
assert result.ret == ExitCode.USAGE_ERROR