diff --git a/changelog/4051.trivial.rst b/changelog/4051.trivial.rst new file mode 100644 index 000000000..401f3b1a8 --- /dev/null +++ b/changelog/4051.trivial.rst @@ -0,0 +1 @@ +Imrpove Error messaging when Invalid Python Syntax is passed through the ``-m`` option to limit what marks to run against. diff --git a/src/_pytest/mark/legacy.py b/src/_pytest/mark/legacy.py index ab016a035..0d0d852ce 100644 --- a/src/_pytest/mark/legacy.py +++ b/src/_pytest/mark/legacy.py @@ -66,7 +66,10 @@ python_keywords_allowed_list = ["or", "and", "not"] def matchmark(colitem, markexpr): """Tries to match on any marker names, attached to the given colitem.""" - return eval(markexpr, {}, MarkMapping.from_item(colitem)) + try: + return eval(markexpr, {}, MarkMapping.from_item(colitem)) + except SyntaxError as e: + raise SyntaxError(str(e) + "\nMarker expression must be valid Python!") def matchkeyword(colitem, keywordexpr): diff --git a/testing/test_mark.py b/testing/test_mark.py index 9dad7a165..cc77d2682 100644 --- a/testing/test_mark.py +++ b/testing/test_mark.py @@ -799,6 +799,18 @@ class TestFunctional(object): deselected_tests = dlist[0].items assert len(deselected_tests) == 2 + def test_invalid_m_option(self, testdir): + testdir.makepyfile( + """ + def test_a(): + pass + """ + ) + result = testdir.runpytest("-m bogus/") + result.stdout.fnmatch_lines( + ["INTERNALERROR> Marker expression must be valid Python!"] + ) + def test_keywords_at_node_level(self, testdir): testdir.makepyfile( """