Skip to content

Commit 563829d

Browse files
[3.11] gh-103186: Fix or catch 'extra' stderr output from unittests (#103196) (#106606)
Reduce test noise by fixing or catching and testing stderr messages from individual tests. test_cmd_line_script.test_script_as_dev_fd calls spawn_python and hence subprocess.Popen with incompatible arguments. On POSIX, pass_fds forces close_fds to be True (subprocess.py line 848). Correct the call. test_uuid.test_cli_namespace_required_for_uuid3: when the namespace is omitted, uuid.main calls argparse.Argument_Parser.error, which prints to stderr before calling sys.exit, which raises SystemExit. Unittest assertRaises catches the exception but not the previous output. Catch the output and test it. test_warnings.test_catchwarnings_with_simplefilter_error similarly prints before raising. Catch the output and test it. --------- Co-authored-by: Oleg Iarygin <[email protected]> (cherry picked from commit 9d58225)
1 parent dd04697 commit 563829d

File tree

2 files changed

+8
-4
lines changed

2 files changed

+8
-4
lines changed

Lib/test/test_cmd_line_script.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -777,7 +777,7 @@ def test_script_as_dev_fd(self):
777777
with os_helper.temp_dir() as work_dir:
778778
script_name = _make_test_script(work_dir, 'script.py', script)
779779
with open(script_name, "r") as fp:
780-
p = spawn_python(f"/dev/fd/{fp.fileno()}", close_fds=False, pass_fds=(0,1,2,fp.fileno()))
780+
p = spawn_python(f"/dev/fd/{fp.fileno()}", close_fds=True, pass_fds=(0,1,2,fp.fileno()))
781781
out, err = p.communicate()
782782
self.assertEqual(out, b"12345678912345678912345\n")
783783

Lib/test/test_warnings/__init__.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -388,9 +388,13 @@ def test_catchwarnings_with_simplefilter_error(self):
388388
with self.module.catch_warnings(
389389
module=self.module, action="error", category=FutureWarning
390390
):
391-
self.module.warn("Other types of warnings are not errors")
392-
self.assertRaises(FutureWarning,
393-
self.module.warn, FutureWarning("msg"))
391+
with support.captured_stderr() as stderr:
392+
error_msg = "Other types of warnings are not errors"
393+
self.module.warn(error_msg)
394+
self.assertRaises(FutureWarning,
395+
self.module.warn, FutureWarning("msg"))
396+
stderr = stderr.getvalue()
397+
self.assertIn(error_msg, stderr)
394398

395399
class CFilterTests(FilterTests, unittest.TestCase):
396400
module = c_warnings

0 commit comments

Comments
 (0)