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

Emit warning on unknown marks via decorator #4933

Closed
wants to merge 4 commits into from

Conversation

Zac-HD
Copy link
Member

@Zac-HD Zac-HD commented Mar 15, 2019

One of the most common problems I see, and help debug, is typos in @pytest.mark.whatever decorators. Just recently we've had #4639 and #4814, and I've read three sets of unrelated docs in the last week that explicitly point out the spelling of mark.parametrize (not parameterize!).

The --strict argument helps, but is clearly insufficient by default. On the other hand, it can't be an error until a major version bump. This PR emits a warning every time an unknown name is used as an attribute of pytest.mark, and therefore closes #4826.

TODO: tests, if people otherwise like the approach.

@codecov
Copy link

codecov bot commented Mar 15, 2019

Codecov Report

Merging #4933 into master will decrease coverage by 3.43%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #4933      +/-   ##
==========================================
- Coverage   95.68%   92.25%   -3.44%     
==========================================
  Files         113      113              
  Lines       25178    25174       -4     
  Branches     2498     2498              
==========================================
- Hits        24092    23224     -868     
- Misses        767     1605     +838     
- Partials      319      345      +26
Impacted Files Coverage Δ
src/_pytest/warning_types.py 100% <100%> (ø) ⬆️
src/_pytest/mark/structures.py 92.44% <100%> (-0.18%) ⬇️
src/_pytest/mark/__init__.py 97.53% <100%> (-0.04%) ⬇️
testing/test_pdb.py 40.78% <0%> (-58.3%) ⬇️
testing/test_argcomplete.py 20.28% <0%> (-47.83%) ⬇️
src/_pytest/_argcomplete.py 33.33% <0%> (-41.67%) ⬇️
src/_pytest/debugging.py 47.16% <0%> (-30.82%) ⬇️
testing/python/approx.py 80.45% <0%> (-19.18%) ⬇️
src/_pytest/unittest.py 76.19% <0%> (-17.99%) ⬇️
testing/code/test_excinfo.py 83.16% <0%> (-13.64%) ⬇️
... and 33 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 1584322...9b26a1a. Read the comment docs.

doc/en/mark.rst Show resolved Hide resolved

Marks added by pytest or by a plugin instead of the decorator will not trigger
the warning or this error. Test suites that want to enforce a limited set of
markers can add ``--strict`` to ``addopts``:
Copy link
Member

Choose a reason for hiding this comment

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

Can we introduce --strict-marks as an alias to --strict?

Historically --strict started with marks but it was intended to also turn warnings into errors, but those plans have changed now that we use standard warnings, so I think it makes sense to make that explicit in the option name. Thoughts?

Copy link
Member

Choose a reason for hiding this comment

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

i like to see --strict go away as the current form is simply very lacking

Copy link
Member Author

Choose a reason for hiding this comment

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

I'd prefer to leave that to a future PR, since I don't have time to do that too at the moment.

Copy link
Member

Choose a reason for hiding this comment

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

Fair enough! 👍

@@ -9,6 +9,14 @@ class PytestWarning(UserWarning):
"""


class UnknownMarkWarning(PytestWarning):
Copy link
Member

Choose a reason for hiding this comment

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

About creating a new warning, see my thoughts on the bottom of #4830 (comment).

cc @blueyed

If we go down that route, we should introduce warning types for the other PytestWarning types we produce (I think there are just a few).

Copy link
Member

Choose a reason for hiding this comment

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

If we go down that route, we should introduce warning types for the other PytestWarning types we produce (I think there are just a few).

Just to be clear: if we decide to add more warning types, we should do it in a separate PR so as not to burden this one.

Copy link
Member Author

Choose a reason for hiding this comment

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

OK, taking it out of this one.

@@ -187,6 +187,9 @@ filterwarnings =
# Do not cause SyntaxError for invalid escape sequences in py37.
default:invalid escape sequence:DeprecationWarning
pytester_example_dir = testing/example_scripts
markers =
issue
pytester_example_path
Copy link
Member

Choose a reason for hiding this comment

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

This marker should be registered by pytester I think

@@ -284,27 +285,48 @@ def test_function():

_config = None

# These marks are always treated as known for the warning, which is designed
Copy link
Member

Choose a reason for hiding this comment

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

I suppose the proper way to handle this is to make each plugin to register their known marks, just like any plugin (for example skipping.py should register skip, skipif and xfail markers).

Copy link
Member Author

Choose a reason for hiding this comment

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

🤦‍♂️ They already do, actually. That'll simplify the implementation a bit...

@nicoddemus
Copy link
Member

nicoddemus commented Mar 15, 2019

Looking good so far! Please see my comments. 👍

@blueyed
Copy link
Contributor

blueyed commented Mar 15, 2019

This should target features.

I've noticed strange output for failures: https://travis-ci.org/pytest-dev/pytest/jobs/506730858#L877-L900

FAILED testing/test_capture.py
FAILED testing/test_capture.py
FAILED testing/test_conftest.py
FAILED testing/test_conftest.py
FAILED testing/test_mark.py
FAILED testing/test_mark.py
FAILED testing/code/test_code.py
FAILED testing/code/test_code.py
FAILED testing/code/test_excinfo.py
FAILED testing/code/test_excinfo.py
FAILED testing/code/test_source.py
FAILED testing/code/test_source.py
FAILED testing/python/fixture.py
FAILED testing/python/fixture.py
FAILED testing/python/integration.py
FAILED testing/python/integration.py
FAILED testing/python/metafunc.py
FAILED testing/python/metafunc.py
FAILED testing/test_skipping.py::TestEvaluator::test_marked_no_args
FAILED testing/test_skipping.py::TestEvaluator::test_marked_one_arg
FAILED testing/test_skipping.py::TestEvaluator::test_marked_one_arg_with_reason
FAILED testing/test_terminal.py::TestTerminalFunctional::test_show_deselected_items_using_markexpr_before_test_execution
FAILED testing/python/collect.py::TestFunction::test_parametrize_with_mark

@Zac-HD
Copy link
Member Author

Zac-HD commented Mar 16, 2019

Thanks everyone for your comments! I'll take them on board, and open a new PR against features.

@blueyed
Copy link
Contributor

blueyed commented Mar 16, 2019

open a new PR against features.

JFI: you can edit the base branch (top right, with the title) of a PR - and then force-push the rebased one.

@Zac-HD
Copy link
Member Author

Zac-HD commented Mar 16, 2019

Oh, awesome! I never realised that was for more than just the title, thanks 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Emit warnings on use of unknown marks
4 participants