Skip to content

Commit e6e300e

Browse files
authored
Merge pull request #7396 from gnikonorov/issue_7295
Refactor src/_pytest/config/__init__.py to use the warnings module instead of stderr for warnings
2 parents 992a7a8 + 49ec2ae commit e6e300e

File tree

4 files changed

+68
-31
lines changed

4 files changed

+68
-31
lines changed

changelog/7295.trivial.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
``src/_pytest/config/__init__.py`` now uses the ``warnings`` module to report warnings instead of ``sys.stderr.write``.

src/_pytest/config/__init__.py

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1038,7 +1038,7 @@ def _consider_importhook(self, args: Sequence[str]) -> None:
10381038
mode = "plain"
10391039
else:
10401040
self._mark_plugins_for_rewrite(hook)
1041-
_warn_about_missing_assertion(mode)
1041+
self._warn_about_missing_assertion(mode)
10421042

10431043
def _mark_plugins_for_rewrite(self, hook) -> None:
10441044
"""
@@ -1182,7 +1182,12 @@ def _validate_plugins(self) -> None:
11821182
def _warn_or_fail_if_strict(self, message: str) -> None:
11831183
if self.known_args_namespace.strict_config:
11841184
fail(message, pytrace=False)
1185-
sys.stderr.write("WARNING: {}".format(message))
1185+
1186+
from _pytest.warnings import _issue_warning_captured
1187+
1188+
_issue_warning_captured(
1189+
PytestConfigWarning(message), self.hook, stacklevel=3,
1190+
)
11861191

11871192
def _get_unknown_ini_keys(self) -> List[str]:
11881193
parser_inicfg = self._parser._inidict
@@ -1351,6 +1356,28 @@ def getvalueorskip(self, name: str, path=None):
13511356
""" (deprecated, use getoption(skip=True)) """
13521357
return self.getoption(name, skip=True)
13531358

1359+
def _warn_about_missing_assertion(self, mode: str) -> None:
1360+
if not _assertion_supported():
1361+
from _pytest.warnings import _issue_warning_captured
1362+
1363+
if mode == "plain":
1364+
warning_text = (
1365+
"ASSERTIONS ARE NOT EXECUTED"
1366+
" and FAILING TESTS WILL PASS. Are you"
1367+
" using python -O?"
1368+
)
1369+
else:
1370+
warning_text = (
1371+
"assertions not in test modules or"
1372+
" plugins will be ignored"
1373+
" because assert statements are not executed "
1374+
"by the underlying Python interpreter "
1375+
"(are you using python -O?)\n"
1376+
)
1377+
_issue_warning_captured(
1378+
PytestConfigWarning(warning_text), self.hook, stacklevel=3,
1379+
)
1380+
13541381

13551382
def _assertion_supported() -> bool:
13561383
try:
@@ -1361,24 +1388,6 @@ def _assertion_supported() -> bool:
13611388
return False
13621389

13631390

1364-
def _warn_about_missing_assertion(mode) -> None:
1365-
if not _assertion_supported():
1366-
if mode == "plain":
1367-
sys.stderr.write(
1368-
"WARNING: ASSERTIONS ARE NOT EXECUTED"
1369-
" and FAILING TESTS WILL PASS. Are you"
1370-
" using python -O?"
1371-
)
1372-
else:
1373-
sys.stderr.write(
1374-
"WARNING: assertions not in test modules or"
1375-
" plugins will be ignored"
1376-
" because assert statements are not executed "
1377-
"by the underlying Python interpreter "
1378-
"(are you using python -O?)\n"
1379-
)
1380-
1381-
13821391
def create_terminal_writer(
13831392
config: Config, file: Optional[TextIO] = None
13841393
) -> TerminalWriter:

testing/test_assertion.py

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1384,12 +1384,35 @@ def test_multitask_job():
13841384

13851385

13861386
@pytest.mark.skipif("'__pypy__' in sys.builtin_module_names")
1387-
def test_warn_missing(testdir):
1387+
@pytest.mark.parametrize(
1388+
"cmdline_args, warning_output",
1389+
[
1390+
(
1391+
["-OO", "-m", "pytest", "-h"],
1392+
["warning :*PytestConfigWarning:*assert statements are not executed*"],
1393+
),
1394+
(
1395+
["-OO", "-m", "pytest"],
1396+
[
1397+
"=*= warnings summary =*=",
1398+
"*PytestConfigWarning:*assert statements are not executed*",
1399+
],
1400+
),
1401+
(
1402+
["-OO", "-m", "pytest", "--assert=plain"],
1403+
[
1404+
"=*= warnings summary =*=",
1405+
"*PytestConfigWarning: ASSERTIONS ARE NOT EXECUTED and FAILING TESTS WILL PASS. "
1406+
"Are you using python -O?",
1407+
],
1408+
),
1409+
],
1410+
)
1411+
def test_warn_missing(testdir, cmdline_args, warning_output):
13881412
testdir.makepyfile("")
1389-
result = testdir.run(sys.executable, "-OO", "-m", "pytest", "-h")
1390-
result.stderr.fnmatch_lines(["*WARNING*assert statements are not executed*"])
1391-
result = testdir.run(sys.executable, "-OO", "-m", "pytest")
1392-
result.stderr.fnmatch_lines(["*WARNING*assert statements are not executed*"])
1413+
1414+
result = testdir.run(sys.executable, *cmdline_args)
1415+
result.stdout.fnmatch_lines(warning_output)
13931416

13941417

13951418
def test_recursion_source_decode(testdir):

testing/test_config.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ def test_confcutdir(self, testdir):
164164
assert result.ret == 0
165165

166166
@pytest.mark.parametrize(
167-
"ini_file_text, invalid_keys, stderr_output, exception_text",
167+
"ini_file_text, invalid_keys, warning_output, exception_text",
168168
[
169169
(
170170
"""
@@ -174,8 +174,9 @@ def test_confcutdir(self, testdir):
174174
""",
175175
["unknown_ini", "another_unknown_ini"],
176176
[
177-
"WARNING: Unknown config ini key: another_unknown_ini",
178-
"WARNING: Unknown config ini key: unknown_ini",
177+
"=*= warnings summary =*=",
178+
"*PytestConfigWarning:*Unknown config ini key: another_unknown_ini",
179+
"*PytestConfigWarning:*Unknown config ini key: unknown_ini",
179180
],
180181
"Unknown config ini key: another_unknown_ini",
181182
),
@@ -186,7 +187,10 @@ def test_confcutdir(self, testdir):
186187
minversion = 5.0.0
187188
""",
188189
["unknown_ini"],
189-
["WARNING: Unknown config ini key: unknown_ini"],
190+
[
191+
"=*= warnings summary =*=",
192+
"*PytestConfigWarning:*Unknown config ini key: unknown_ini",
193+
],
190194
"Unknown config ini key: unknown_ini",
191195
),
192196
(
@@ -221,7 +225,7 @@ def test_confcutdir(self, testdir):
221225
],
222226
)
223227
def test_invalid_ini_keys(
224-
self, testdir, ini_file_text, invalid_keys, stderr_output, exception_text
228+
self, testdir, ini_file_text, invalid_keys, warning_output, exception_text
225229
):
226230
testdir.makeconftest(
227231
"""
@@ -235,7 +239,7 @@ def pytest_addoption(parser):
235239
assert sorted(config._get_unknown_ini_keys()) == sorted(invalid_keys)
236240

237241
result = testdir.runpytest()
238-
result.stderr.fnmatch_lines(stderr_output)
242+
result.stdout.fnmatch_lines(warning_output)
239243

240244
if exception_text:
241245
with pytest.raises(pytest.fail.Exception, match=exception_text):

0 commit comments

Comments
 (0)