Skip to contentSkip to conversation

kunit: tool: suppress confusing error output in test

PATCH In review Jinseok Kim 2 participants 3 messages Jan 12 – Feb 1

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 kunit: tool: suppress confusing error output in test

When running kunit_tool_test.py, the test_run_raw_output_invalid test prints invalid usage text and error messages to stderr. This happens because the test triggers kunit.main() with an invalid argument, causing argparse to print the usage and exit.

This output is confusing to the user because it looks like a test failure or usage error, even though the test passed successfully. Furthermore, argparse displays 'usage: kunit_tool_test.py run ...', which is misleading since the test script itself does not accept the 'run' command.

This patch mocks sys.stderr in the test to suppress this expected error output, making the test execution output cleaner.

---
 tools/testing/kunit/kunit_tool_test.py | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
tools/testing/kunit/kunit_tool_test.py +4 -2
index bbba921e0eac..7a8ba8e4f1d4 100755
@@ -8,6 +8,7 @@
 
 import unittest
 from unittest import mock
+import io
 
 import tempfile, shutil # Handling test_tmpdir
 
@@ -688,8 +689,9 @@ class KUnitMainTest(unittest.TestCase):
 
 	def test_run_raw_output_invalid(self):
 		self.linux_source_mock.run_kernel = mock.Mock(return_value=[])
-		with self.assertRaises(SystemExit) as e:
-			kunit.main(['run', '--raw_output=invalid'])
+		with mock.patch('sys.stderr', new=io.StringIO()):
+			with self.assertRaises(SystemExit) as e:
+				kunit.main(['run', '--raw_output=invalid'])
 		self.assertNotEqual(e.exception.code, 0)
 
 	def test_run_raw_output_does_not_take_positional_args(self):

--
2.43.0

Reviewed-by on v1 1/1
#1 lore raw
Quoted 11 lines · Jinseok Kim

When running kunit_tool_test.py, the test_run_raw_output_invalid test prints invalid usage text and error messages to stderr. This happens because the test triggers kunit.main() with an invalid argument, causing argparse to print the usage and exit.

This output is confusing to the user because it looks like a test failure or usage error, even though the test passed successfully. Furthermore, argparse displays 'usage: kunit_tool_test.py run ...', which is misleading since the test script itself does not accept the 'run' command.

Quoted 5 lines

This patch mocks sys.stderr in the test to suppress this expected error output, making the test execution output cleaner.

---

This has been slightly annoying for a long time, so thanks for fixing it.

One minor nitpick in the commit description, otherwise this is good:

WARNING: Prefer a maximum 75 chars per line (possible unwrapped commit description?)
#11:
prints invalid usage text and error messages to stderr. This happens because the

Cheers,
-- David

Quoted 29 lines
 tools/testing/kunit/kunit_tool_test.py | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
tools/testing/kunit/kunit_tool_test.py +4 -3
index bbba921e0eac..7a8ba8e4f1d4 100755
@@ -8,6 +8,7 @@
 
 import unittest
 from unittest import mock
+import io
 
 import tempfile, shutil # Handling test_tmpdir
 
@@ -688,8 +689,9 @@ class KUnitMainTest(unittest.TestCase):
 
        def test_run_raw_output_invalid(self):
                self.linux_source_mock.run_kernel = mock.Mock(return_value=[])
-               with self.assertRaises(SystemExit) as e:
-                       kunit.main(['run', '--raw_output=invalid'])
+               with mock.patch('sys.stderr', new=io.StringIO()):
+                       with self.assertRaises(SystemExit) as e:
+                               kunit.main(['run', '--raw_output=invalid'])
                self.assertNotEqual(e.exception.code, 0)
 
        def test_run_raw_output_does_not_take_positional_args(self):
--

2.43.0

#2 lore raw

PATCH v2 1/1 kunit: reworded commit message

In reply to Jinseok Kim

When running kunit_tool_test.py, the test_run_raw_output_invalid test prints invalid usage text and error messages to stderr. This happens because the test triggers kunit.main() with an invalid argument, causing argparse to print the usage and exit.

This output is confusing to the user because it looks like a test failure or usage error, even though the test passed successfully. Furthermore, argparse displays 'usage: kunit_tool_test.py run ...', which is misleading since the test script itself does not accept the 'run' command.

This patch mocks sys.stderr in the test to suppress this expected error output, making the test execution output cleaner.

Changes in v2:
- Reworded commit message to keep lines less than 75 characters

---
 tools/testing/kunit/kunit_tool_test.py | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
tools/testing/kunit/kunit_tool_test.py +2 -2
index b5cc07c3d027..7a8ba8e4f1d4 100755
@@ -690,8 +690,8 @@ class KUnitMainTest(unittest.TestCase):
 	def test_run_raw_output_invalid(self):
 		self.linux_source_mock.run_kernel = mock.Mock(return_value=[])
 		with mock.patch('sys.stderr', new=io.StringIO()):
-            with self.assertRaises(SystemExit) as e:
-                kunit.main(['run', '--raw_output=invalid'])
+			with self.assertRaises(SystemExit) as e:
+				kunit.main(['run', '--raw_output=invalid'])
 		self.assertNotEqual(e.exception.code, 0)
 
 	def test_run_raw_output_does_not_take_positional_args(self):

--
2.43.0