-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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 #4935
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
A warning is now emitted when unknown marks are used as a decorator. | ||
This is often due to a typo, which can lead to silently broken tests. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,7 @@ | |
from ..compat import MappingMixin | ||
from ..compat import NOTSET | ||
from _pytest.outcomes import fail | ||
from _pytest.warning_types import UnknownMarkWarning | ||
|
||
EMPTY_PARAMETERSET_OPTION = "empty_parameter_set_mark" | ||
|
||
|
@@ -283,28 +284,38 @@ def test_function(): | |
on the ``test_function`` object. """ | ||
|
||
_config = None | ||
_markers = set() | ||
|
||
def __getattr__(self, name): | ||
if name[0] == "_": | ||
raise AttributeError("Marker name must NOT start with underscore") | ||
|
||
if self._config is not None: | ||
self._check(name) | ||
return MarkDecorator(Mark(name, (), {})) | ||
# We store a set of markers as a performance optimisation - if a mark | ||
# name is in the set we definitely know it, but a mark may be known and | ||
# not in the set. We therefore start by updating the set! | ||
if name not in self._markers: | ||
for line in self._config.getini("markers"): | ||
# example lines: "skipif(condition): skip the given test if..." | ||
# or "hypothesis: tests which use Hypothesis", so to get the | ||
# marker name we we split on both `:` and `(`. | ||
marker = line.split(":")[0].split("(")[0].strip() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This used |
||
self._markers.add(marker) | ||
|
||
# If the name is not in the set of known marks after updating, | ||
# then it really is time to issue a warning or an error. | ||
if name not in self._markers: | ||
if self._config.option.strict: | ||
nicoddemus marked this conversation as resolved.
Show resolved
Hide resolved
|
||
fail("{!r} not a registered marker".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, | ||
UnknownMarkWarning, | ||
) | ||
|
||
def _check(self, name): | ||
try: | ||
if name in self._markers: | ||
return | ||
except AttributeError: | ||
pass | ||
self._markers = values = set() | ||
for line in self._config.getini("markers"): | ||
marker = line.split(":", 1)[0] | ||
marker = marker.rstrip() | ||
x = marker.split("(", 1)[0] | ||
values.add(x) | ||
if name not in self._markers: | ||
fail("{!r} not a registered marker".format(name), pytrace=False) | ||
return MarkDecorator(Mark(name, (), {})) | ||
|
||
|
||
MARK_GEN = MarkGenerator() | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When can it happen that it is known, but not in the set? (except for the first read of the config of course)
(I've notied that the config is re-processed again for every unknown mark, whereas previously it was "read" just once)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this covers the case where marks can be registered later, for example by
pytest_configure
hooks inconftests.py
not at the root.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep,
pytest_configure
can run later than I had expected.The re-processing isn't free, but the number of unknown marks should be relatively small and it's necessary for correctness.