From ac17f20d98197606791ce5602d23307a53aa11fe Mon Sep 17 00:00:00 2001 From: Brianna Laugher Date: Sat, 21 Mar 2015 23:06:25 +0100 Subject: [PATCH 1/6] #463 Raise a ValueError early if user misspells 'parametrize' as 'parameterize'. --HG-- branch : issue463 --- _pytest/python.py | 7 +++++++ testing/python/metafunc.py | 15 +++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/_pytest/python.py b/_pytest/python.py index 318038771..e12664378 100644 --- a/_pytest/python.py +++ b/_pytest/python.py @@ -142,6 +142,13 @@ def pytest_cmdline_main(config): def pytest_generate_tests(metafunc): + try: + # this misspelling is common - raise a specific error to alert the user + markers = metafunc.function.parameterize + msg = "{} has mark 'parameterize', spelling should be 'parametrize'" + raise ValueError(msg.format(metafunc.function.__name__)) + except AttributeError: + pass try: markers = metafunc.function.parametrize except AttributeError: diff --git a/testing/python/metafunc.py b/testing/python/metafunc.py index 651e3846e..c1a573a47 100644 --- a/testing/python/metafunc.py +++ b/testing/python/metafunc.py @@ -692,6 +692,21 @@ class TestMetafuncFunctional: reprec = testdir.inline_run() reprec.assertoutcome(passed=4) + @pytest.mark.issue463 + def test_parameterize_misspelling(self, testdir): + testdir.makepyfile(""" + import pytest + + @pytest.mark.parameterize("x", range(2)) + def test_foo(x): + pass + """) + reprec = testdir.inline_run('--collectonly') + failures = reprec.getfailures() + assert len(failures) == 1 + expectederror = "ValueError: test_foo has mark 'parameterize', spelling should be 'parametrize'" + assert expectederror in failures[0].longrepr.reprcrash.message + class TestMarkersWithParametrization: pytestmark = pytest.mark.issue308 From 6f81602ba2ecffbc80074e8fc67adc24ada974a9 Mon Sep 17 00:00:00 2001 From: Brianna Laugher Date: Sat, 21 Mar 2015 23:30:13 +0100 Subject: [PATCH 2/6] Use hasattr instead of try/except --HG-- branch : issue463 --- _pytest/python.py | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/_pytest/python.py b/_pytest/python.py index e12664378..4681c6a7b 100644 --- a/_pytest/python.py +++ b/_pytest/python.py @@ -142,13 +142,10 @@ def pytest_cmdline_main(config): def pytest_generate_tests(metafunc): - try: - # this misspelling is common - raise a specific error to alert the user - markers = metafunc.function.parameterize + # this misspelling is common - raise a specific error to alert the user + if hasattr(metafunc.function, 'parameterize'): msg = "{} has mark 'parameterize', spelling should be 'parametrize'" raise ValueError(msg.format(metafunc.function.__name__)) - except AttributeError: - pass try: markers = metafunc.function.parametrize except AttributeError: From deb163d237840b5f5dab57ead64a657351ea95e2 Mon Sep 17 00:00:00 2001 From: Brianna Laugher Date: Sat, 21 Mar 2015 23:57:06 +0100 Subject: [PATCH 3/6] Change string format syntax from {} to {0} for py2.6 --HG-- branch : issue463 --- _pytest/python.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_pytest/python.py b/_pytest/python.py index 4681c6a7b..4d57a3c4b 100644 --- a/_pytest/python.py +++ b/_pytest/python.py @@ -144,7 +144,7 @@ def pytest_cmdline_main(config): def pytest_generate_tests(metafunc): # this misspelling is common - raise a specific error to alert the user if hasattr(metafunc.function, 'parameterize'): - msg = "{} has mark 'parameterize', spelling should be 'parametrize'" + msg = "{0} has mark 'parameterize', spelling should be 'parametrize'" raise ValueError(msg.format(metafunc.function.__name__)) try: markers = metafunc.function.parametrize From 43e4fcf6dddc6130ff31668b3101dc53de46c9f1 Mon Sep 17 00:00:00 2001 From: Brianna Laugher Date: Mon, 23 Mar 2015 20:01:58 +0100 Subject: [PATCH 4/6] Raise specific MarkerError rather than generic ValueError --HG-- branch : issue463 --- _pytest/mark.py | 3 +++ _pytest/python.py | 6 +++--- testing/python/metafunc.py | 2 +- 3 files changed, 7 insertions(+), 4 deletions(-) diff --git a/_pytest/mark.py b/_pytest/mark.py index b9dbe6a7c..06f961496 100644 --- a/_pytest/mark.py +++ b/_pytest/mark.py @@ -1,6 +1,9 @@ """ generic mechanism for marking and selecting python functions. """ import py +class MarkerError(Exception): + """Error in use of a pytest marker/attribute""" + def pytest_namespace(): return {'mark': MarkGenerator()} diff --git a/_pytest/python.py b/_pytest/python.py index 4d57a3c4b..596395062 100644 --- a/_pytest/python.py +++ b/_pytest/python.py @@ -4,7 +4,7 @@ import py import inspect import sys import pytest -from _pytest.mark import MarkDecorator +from _pytest.mark import MarkDecorator, MarkerError from py._code.code import TerminalRepr import _pytest @@ -144,8 +144,8 @@ def pytest_cmdline_main(config): def pytest_generate_tests(metafunc): # this misspelling is common - raise a specific error to alert the user if hasattr(metafunc.function, 'parameterize'): - msg = "{0} has mark 'parameterize', spelling should be 'parametrize'" - raise ValueError(msg.format(metafunc.function.__name__)) + msg = "{0} has 'parameterize', spelling should be 'parametrize'" + raise MarkerError(msg.format(metafunc.function.__name__)) try: markers = metafunc.function.parametrize except AttributeError: diff --git a/testing/python/metafunc.py b/testing/python/metafunc.py index c1a573a47..cbff9adfe 100644 --- a/testing/python/metafunc.py +++ b/testing/python/metafunc.py @@ -704,7 +704,7 @@ class TestMetafuncFunctional: reprec = testdir.inline_run('--collectonly') failures = reprec.getfailures() assert len(failures) == 1 - expectederror = "ValueError: test_foo has mark 'parameterize', spelling should be 'parametrize'" + expectederror = "MarkerError: test_foo has 'parameterize', spelling should be 'parametrize'" assert expectederror in failures[0].longrepr.reprcrash.message From c019e489d28ae093b3f92f8edcf3ebd7bbd54803 Mon Sep 17 00:00:00 2001 From: Brianna Laugher Date: Mon, 23 Mar 2015 20:27:53 +0100 Subject: [PATCH 5/6] Change docstring style --HG-- branch : issue463 --- _pytest/mark.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/_pytest/mark.py b/_pytest/mark.py index 06f961496..a619f96ca 100644 --- a/_pytest/mark.py +++ b/_pytest/mark.py @@ -2,7 +2,9 @@ import py class MarkerError(Exception): - """Error in use of a pytest marker/attribute""" + """ + Error in use of a pytest marker/attribute. + """ def pytest_namespace(): From 3ba3112a8552108acabd248c59601d4866a35863 Mon Sep 17 00:00:00 2001 From: Brianna Laugher Date: Mon, 23 Mar 2015 20:28:04 +0100 Subject: [PATCH 6/6] update changelog --HG-- branch : issue463 --- CHANGELOG | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG b/CHANGELOG index 20a86f0f1..7ef444b11 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,6 +1,8 @@ 2.7.0.dev (compared to 2.6.4) ----------------------------- +- fix issue463: raise specific error for 'parameterize' misspelling. + - fix issue616: conftest.py files and their contained fixutres are now properly considered for visibility, independently from the exact current working directory and test arguments that are used.