Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve check for misspelling of parametrize #6231

Merged
merged 1 commit into from Nov 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog/6231.improvement.rst
@@ -0,0 +1 @@
Improve check for misspelling of ``pytest.mark.parametrize``.
19 changes: 12 additions & 7 deletions src/_pytest/mark/structures.py
Expand Up @@ -314,13 +314,18 @@ def __getattr__(self, name: str) -> MarkDecorator:
"{!r} not found in `markers` configuration option".format(name),
pytrace=False,
)
else:
warnings.warn(
"Unknown pytest.mark.%s - is this a typo? You can register "
"custom marks to avoid this warning - for details, see "
"https://docs.pytest.org/en/latest/mark.html" % name,
PytestUnknownMarkWarning,
)

# Raise a specific error for common misspellings of "parametrize".
if name in ["parameterize", "parametrise", "parameterise"]:
__tracebackhide__ = True
fail("Unknown '{}' mark, did you mean 'parametrize'?".format(name))

warnings.warn(
"Unknown pytest.mark.%s - is this a typo? You can register "
"custom marks to avoid this warning - for details, see "
"https://docs.pytest.org/en/latest/mark.html" % name,
PytestUnknownMarkWarning,
)

return MarkDecorator(Mark(name, (), {}))

Expand Down
10 changes: 0 additions & 10 deletions src/_pytest/python.py
Expand Up @@ -120,17 +120,7 @@ def pytest_cmdline_main(config):
return 0


def _validate_parametrize_spelling(metafunc):
"""Raise a specific error for common misspellings of "parametrize"."""
for mark_name in ["parameterize", "parametrise", "parameterise"]:
if metafunc.definition.get_closest_marker(mark_name):
msg = "{0} has '{1}' mark, spelling should be 'parametrize'"
fail(msg.format(metafunc.function.__name__, mark_name), pytrace=False)


def pytest_generate_tests(metafunc):
_validate_parametrize_spelling(metafunc)

for marker in metafunc.definition.iter_markers(name="parametrize"):
metafunc.parametrize(*marker.args, **marker.kwargs)

Expand Down
20 changes: 12 additions & 8 deletions testing/python/metafunc.py
Expand Up @@ -1323,25 +1323,29 @@ def test_foo(x):
reprec = testdir.runpytest()
reprec.assert_outcomes(passed=4)

@pytest.mark.parametrize("attr", ["parametrise", "parameterize", "parameterise"])
def test_parametrize_misspelling(self, testdir, attr):
def test_parametrize_misspelling(self, testdir):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i believe we can now or as a follow-up turn this into a unittest (and leave the checks for all misspellings)

after all its now a with pytest.raises(pytest.fail.Exception): pytest.mark.parametrise/...

Copy link
Contributor Author

@blueyed blueyed Nov 19, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@RonnyPfannschmidt
FWIW I think it's not worth it: it's good to have it as an integration test in general, but it's enough to test one variant then.

"""#463"""
testdir.makepyfile(
"""
import pytest

@pytest.mark.{}("x", range(2))
@pytest.mark.parametrise("x", range(2))
def test_foo(x):
pass
""".format(
attr
)
"""
)
result = testdir.runpytest("--collectonly")
result.stdout.fnmatch_lines(
[
"test_foo has '{}' mark, spelling should be 'parametrize'".format(attr),
"*1 error in*",
"collected 0 items / 1 error",
"",
"*= ERRORS =*",
"*_ ERROR collecting test_parametrize_misspelling.py _*",
"test_parametrize_misspelling.py:3: in <module>",
' @pytest.mark.parametrise("x", range(2))',
"E Failed: Unknown 'parametrise' mark, did you mean 'parametrize'?",
"*! Interrupted: 1 error during collection !*",
"*= 1 error in *",
]
)

Expand Down