22
33import pytest
44import unittest
5+
6+ from contextlib import redirect_stdout
7+ from io import StringIO
58from unittest import mock
69
710from chipflow_lib import ChipFlowError
@@ -64,17 +67,15 @@ def test_run_command_error(self, mock_get_cls, mock_pin_command, mock_parse_conf
6467 mock_get_cls .return_value = lambda config : mock_test_cmd
6568
6669 # Capture stdout for assertion
67- with mock . patch ( "builtins.print" ) as mock_print :
70+ with redirect_stdout ( StringIO ()) as buffer :
6871 with pytest .raises (SystemExit ) as systemexit :
6972 # Run with error action
7073 run (["test" , "error" ])
7174
7275 assert systemexit .type is SystemExit
7376 assert systemexit .value .code == 1
7477
75- # Error message should be printed
76- mock_print .assert_called_once ()
77- self .assertIn ("Error while executing `test error`" , mock_print .call_args [0 ][0 ])
78+ self .assertIn ("Error while executing `test error`" , buffer .getvalue ())
7879
7980 @mock .patch ("chipflow_lib.cli._parse_config" )
8081 @mock .patch ("chipflow_lib.cli.PinCommand" )
@@ -91,7 +92,7 @@ def test_run_unexpected_error(self, mock_get_cls, mock_pin_command, mock_parse_c
9192 mock_get_cls .return_value = lambda config : mock_test_cmd
9293
9394 # Capture stdout for assertion
94- with mock . patch ( "builtins.print" ) as mock_print :
95+ with redirect_stdout ( StringIO ()) as buffer :
9596 with pytest .raises (SystemExit ) as systemexit :
9697 # Run with unexpected error action
9798 run (["test" , "unexpected" ])
@@ -100,9 +101,8 @@ def test_run_unexpected_error(self, mock_get_cls, mock_pin_command, mock_parse_c
100101 assert systemexit .value .code == 1
101102
102103 # Error message should be printed
103- mock_print .assert_called_once ()
104- self .assertIn ("Error while executing `test unexpected`" , mock_print .call_args [0 ][0 ])
105- self .assertIn ("Unexpected error" , mock_print .call_args [0 ][0 ])
104+ self .assertIn ("Error while executing `test unexpected`" , buffer .getvalue ())
105+ self .assertIn ("Unexpected error" , buffer .getvalue ())
106106
107107 @mock .patch ("chipflow_lib.cli._parse_config" )
108108 @mock .patch ("chipflow_lib.cli.PinCommand" )
0 commit comments