summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>2023-07-10 21:03:32 (GMT)
committerGitHub <noreply@github.com>2023-07-10 21:03:32 (GMT)
commit68ca19061d19345a72574a6c3849981213de212b (patch)
tree1a5ec47a7b7c2a6b5ab8f6776aef0ef6bca11812 /Lib
parent2da967ea14a49d1ca3e2d22d83ce9f6ffd5e6186 (diff)
downloadcpython-68ca19061d19345a72574a6c3849981213de212b.zip
cpython-68ca19061d19345a72574a6c3849981213de212b.tar.gz
cpython-68ca19061d19345a72574a6c3849981213de212b.tar.bz2
[3.12] gh-103186: Fix or catch 'extra' stderr output from unittests (GH-103196) (#106605)
gh-103186: Fix or catch 'extra' stderr output from unittests (GH-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. --------- (cherry picked from commit 9d582250d8fde240b8e7299b74ba888c574f74a3) Co-authored-by: Ijtaba Hussain <ijtabahussain@live.com> Co-authored-by: Oleg Iarygin <oleg@arhadthedev.net>
Diffstat (limited to 'Lib')
-rw-r--r--Lib/test/test_cmd_line_script.py2
-rwxr-xr-xLib/test/test_uuid.py9
-rw-r--r--Lib/test/test_warnings/__init__.py10
3 files changed, 14 insertions, 7 deletions
diff --git a/Lib/test/test_cmd_line_script.py b/Lib/test/test_cmd_line_script.py
index 8bf2993..1b58882 100644
--- a/Lib/test/test_cmd_line_script.py
+++ b/Lib/test/test_cmd_line_script.py
@@ -777,7 +777,7 @@ class CmdLineTest(unittest.TestCase):
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")
diff --git a/Lib/test/test_uuid.py b/Lib/test/test_uuid.py
index a178e94..9cec1e8 100755
--- a/Lib/test/test_uuid.py
+++ b/Lib/test/test_uuid.py
@@ -706,20 +706,23 @@ class BaseTestUUID:
self.assertIs(strong, weak())
@mock.patch.object(sys, "argv", ["", "-u", "uuid3", "-n", "@dns"])
- def test_cli_namespace_required_for_uuid3(self):
+ @mock.patch('sys.stderr', new_callable=io.StringIO)
+ def test_cli_namespace_required_for_uuid3(self, mock_err):
with self.assertRaises(SystemExit) as cm:
self.uuid.main()
# Check that exception code is the same as argparse.ArgumentParser.error
self.assertEqual(cm.exception.code, 2)
+ self.assertIn("error: Incorrect number of arguments", mock_err.getvalue())
@mock.patch.object(sys, "argv", ["", "-u", "uuid3", "-N", "python.org"])
- def test_cli_name_required_for_uuid3(self):
+ @mock.patch('sys.stderr', new_callable=io.StringIO)
+ def test_cli_name_required_for_uuid3(self, mock_err):
with self.assertRaises(SystemExit) as cm:
self.uuid.main()
-
# Check that exception code is the same as argparse.ArgumentParser.error
self.assertEqual(cm.exception.code, 2)
+ self.assertIn("error: Incorrect number of arguments", mock_err.getvalue())
@mock.patch.object(sys, "argv", [""])
def test_cli_uuid4_outputted_with_no_args(self):
diff --git a/Lib/test/test_warnings/__init__.py b/Lib/test/test_warnings/__init__.py
index 9e680c8..83237f5 100644
--- a/Lib/test/test_warnings/__init__.py
+++ b/Lib/test/test_warnings/__init__.py
@@ -387,9 +387,13 @@ class FilterTests(BaseTest):
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