Skip to content

Commit

Permalink
[3.11] pythongh-103186: Fix or catch 'extra' stderr output from unitt…
Browse files Browse the repository at this point in the history
…ests (python#103196) (python#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 <oleg@arhadthedev.net>
(cherry picked from commit 9d58225)
  • Loading branch information
terryjreedy committed Jul 10, 2023
1 parent dd04697 commit 563829d
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Lib/test/test_cmd_line_script.py
Original file line number Diff line number Diff line change
Expand Up @@ -777,7 +777,7 @@ def test_script_as_dev_fd(self):
with os_helper.temp_dir() as work_dir:
script_name = _make_test_script(work_dir, 'script.py', script)
with open(script_name, "r") as fp:
p = spawn_python(f"/dev/fd/{fp.fileno()}", close_fds=False, pass_fds=(0,1,2,fp.fileno()))
p = spawn_python(f"/dev/fd/{fp.fileno()}", close_fds=True, pass_fds=(0,1,2,fp.fileno()))
out, err = p.communicate()
self.assertEqual(out, b"12345678912345678912345\n")

Expand Down
10 changes: 7 additions & 3 deletions Lib/test/test_warnings/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -388,9 +388,13 @@ def test_catchwarnings_with_simplefilter_error(self):
with self.module.catch_warnings(
module=self.module, action="error", category=FutureWarning
):
self.module.warn("Other types of warnings are not errors")
self.assertRaises(FutureWarning,
self.module.warn, FutureWarning("msg"))
with support.captured_stderr() as stderr:
error_msg = "Other types of warnings are not errors"
self.module.warn(error_msg)
self.assertRaises(FutureWarning,
self.module.warn, FutureWarning("msg"))
stderr = stderr.getvalue()
self.assertIn(error_msg, stderr)

class CFilterTests(FilterTests, unittest.TestCase):
module = c_warnings
Expand Down

0 comments on commit 563829d

Please sign in to comment.