From c2179c31279cc9fd78138f4714edaadb1421b5e8 Mon Sep 17 00:00:00 2001 From: Jeffrey Rackauckas Date: Sun, 30 Sep 2018 18:34:56 -0700 Subject: [PATCH 1/2] Improve error messaging when invalid syntax is passed to the -m option --- changelog/4051.trivial | 1 + src/_pytest/mark/legacy.py | 5 ++++- testing/test_mark.py | 12 ++++++++++++ 3 files changed, 17 insertions(+), 1 deletion(-) create mode 100644 changelog/4051.trivial diff --git a/changelog/4051.trivial b/changelog/4051.trivial new file mode 100644 index 000000000..401f3b1a8 --- /dev/null +++ b/changelog/4051.trivial @@ -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 ddeff2789..06bbe65f4 100644 --- a/testing/test_mark.py +++ b/testing/test_mark.py @@ -786,6 +786,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( """ From 7a6d16c1eb6b374386138a5e7f8017ef7ce2ff86 Mon Sep 17 00:00:00 2001 From: Jeffrey Rackauckas Date: Sun, 30 Sep 2018 18:45:49 -0700 Subject: [PATCH 2/2] Adding .rst to end of changelog fragment filename. --- changelog/{4051.trivial => 4051.trivial.rst} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename changelog/{4051.trivial => 4051.trivial.rst} (100%) diff --git a/changelog/4051.trivial b/changelog/4051.trivial.rst similarity index 100% rename from changelog/4051.trivial rename to changelog/4051.trivial.rst