diff --git a/src/_pytest/mark/legacy.py b/src/_pytest/mark/legacy.py index 7581421da..eb50340f2 100644 --- a/src/_pytest/mark/legacy.py +++ b/src/_pytest/mark/legacy.py @@ -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): diff --git a/testing/test_mark.py b/testing/test_mark.py index 69f751012..2aad2b1ba 100644 --- a/testing/test_mark.py +++ b/testing/test_mark.py @@ -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