Merge pull request #7396 from gnikonorov/issue_7295

Refactor src/_pytest/config/__init__.py to use the warnings module instead of stderr for warnings
This commit is contained in:
Ran Benita
2020-06-28 18:02:07 +03:00
committed by GitHub
4 changed files with 68 additions and 31 deletions

View File

@@ -1038,7 +1038,7 @@ class Config:
mode = "plain"
else:
self._mark_plugins_for_rewrite(hook)
_warn_about_missing_assertion(mode)
self._warn_about_missing_assertion(mode)
def _mark_plugins_for_rewrite(self, hook) -> None:
"""
@@ -1182,7 +1182,12 @@ class Config:
def _warn_or_fail_if_strict(self, message: str) -> None:
if self.known_args_namespace.strict_config:
fail(message, pytrace=False)
sys.stderr.write("WARNING: {}".format(message))
from _pytest.warnings import _issue_warning_captured
_issue_warning_captured(
PytestConfigWarning(message), self.hook, stacklevel=3,
)
def _get_unknown_ini_keys(self) -> List[str]:
parser_inicfg = self._parser._inidict
@@ -1351,6 +1356,28 @@ class Config:
""" (deprecated, use getoption(skip=True)) """
return self.getoption(name, skip=True)
def _warn_about_missing_assertion(self, mode: str) -> None:
if not _assertion_supported():
from _pytest.warnings import _issue_warning_captured
if mode == "plain":
warning_text = (
"ASSERTIONS ARE NOT EXECUTED"
" and FAILING TESTS WILL PASS. Are you"
" using python -O?"
)
else:
warning_text = (
"assertions not in test modules or"
" plugins will be ignored"
" because assert statements are not executed "
"by the underlying Python interpreter "
"(are you using python -O?)\n"
)
_issue_warning_captured(
PytestConfigWarning(warning_text), self.hook, stacklevel=3,
)
def _assertion_supported() -> bool:
try:
@@ -1361,24 +1388,6 @@ def _assertion_supported() -> bool:
return False
def _warn_about_missing_assertion(mode) -> None:
if not _assertion_supported():
if mode == "plain":
sys.stderr.write(
"WARNING: ASSERTIONS ARE NOT EXECUTED"
" and FAILING TESTS WILL PASS. Are you"
" using python -O?"
)
else:
sys.stderr.write(
"WARNING: assertions not in test modules or"
" plugins will be ignored"
" because assert statements are not executed "
"by the underlying Python interpreter "
"(are you using python -O?)\n"
)
def create_terminal_writer(
config: Config, file: Optional[TextIO] = None
) -> TerminalWriter: