Clarify mark.__getattr__

This commit is contained in:
Zac Hatfield-Dodds 2019-04-02 12:31:42 +11:00
parent 4f6c67658c
commit cab4069f42
1 changed files with 20 additions and 19 deletions

View File

@ -291,23 +291,9 @@ class MarkGenerator(object):
raise AttributeError("Marker name must NOT start with underscore")
if self._config is not None:
self._update_markers(name)
if name not in self._markers:
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,
)
if self._config.option.strict:
fail("{!r} not a registered marker".format(name), pytrace=False)
return MarkDecorator(Mark(name, (), {}))
def _update_markers(self, name):
# We store a set of registered markers as a performance optimisation,
# but more could be added to `self._config` by other plugins at runtime.
# If we see an unknown marker, we therefore update the set and try again!
# 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..."
@ -316,6 +302,21 @@ class MarkGenerator(object):
marker = line.split(":")[0].split("(")[0].strip()
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:
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,
)
return MarkDecorator(Mark(name, (), {}))
MARK_GEN = MarkGenerator()