Skip to content

Commit 9d58225

Browse files
TabLandarhadthedev
andauthored
gh-103186: Fix or catch 'extra' stderr output from unittests (#103196)
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]>
1 parent a840806 commit 9d58225

File tree

3 files changed

+14
-7
lines changed

3 files changed

+14
-7
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_uuid.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -706,20 +706,23 @@ def test_uuid_weakref(self):
706706
self.assertIs(strong, weak())
707707

708708
@mock.patch.object(sys, "argv", ["", "-u", "uuid3", "-n", "@dns"])
709-
def test_cli_namespace_required_for_uuid3(self):
709+
@mock.patch('sys.stderr', new_callable=io.StringIO)
710+
def test_cli_namespace_required_for_uuid3(self, mock_err):
710711
with self.assertRaises(SystemExit) as cm:
711712
self.uuid.main()
712713

713714
# Check that exception code is the same as argparse.ArgumentParser.error
714715
self.assertEqual(cm.exception.code, 2)
716+
self.assertIn("error: Incorrect number of arguments", mock_err.getvalue())
715717

716718
@mock.patch.object(sys, "argv", ["", "-u", "uuid3", "-N", "python.org"])
717-
def test_cli_name_required_for_uuid3(self):
719+
@mock.patch('sys.stderr', new_callable=io.StringIO)
720+
def test_cli_name_required_for_uuid3(self, mock_err):
718721
with self.assertRaises(SystemExit) as cm:
719722
self.uuid.main()
720-
721723
# Check that exception code is the same as argparse.ArgumentParser.error
722724
self.assertEqual(cm.exception.code, 2)
725+
self.assertIn("error: Incorrect number of arguments", mock_err.getvalue())
723726

724727
@mock.patch.object(sys, "argv", [""])
725728
def test_cli_uuid4_outputted_with_no_args(self):

Lib/test/test_warnings/__init__.py

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

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

0 commit comments

Comments
 (0)