11# SPDX-License-Identifier: BSD-2-Clause
2+
3+ import pytest
24import unittest
35from unittest import mock
46import logging
911
1012class MockCommand :
1113 """Mock command for testing CLI"""
14+
1215 def build_cli_parser (self , parser ):
1316 parser .add_argument ("--option" , help = "Test option" )
1417 parser .add_argument ("action" , choices = ["valid" , "error" , "unexpected" ])
@@ -74,8 +77,12 @@ def test_run_command_error(self, mock_get_cls, mock_pin_command, mock_parse_conf
7477
7578 # Capture stdout for assertion
7679 with mock .patch ("builtins.print" ) as mock_print :
77- # Run with error action
78- run (["test" , "error" ])
80+ with pytest .raises (SystemExit ) as systemexit :
81+ # Run with error action
82+ run (["test" , "error" ])
83+
84+ assert systemexit .type is SystemExit
85+ assert systemexit .value .code == 1
7986
8087 # Error message should be printed
8188 mock_print .assert_called_once ()
@@ -104,8 +111,12 @@ def test_run_unexpected_error(self, mock_get_cls, mock_pin_command, mock_parse_c
104111
105112 # Capture stdout for assertion
106113 with mock .patch ("builtins.print" ) as mock_print :
107- # Run with unexpected error action
108- run (["test" , "unexpected" ])
114+ with pytest .raises (SystemExit ) as systemexit :
115+ # Run with unexpected error action
116+ run (["test" , "unexpected" ])
117+
118+ assert systemexit .type is SystemExit
119+ assert systemexit .value .code == 1
109120
110121 # Error message should be printed
111122 mock_print .assert_called_once ()
@@ -206,4 +217,4 @@ def test_verbosity_flags(self, mock_get_cls, mock_pin_command, mock_parse_config
206217 self .assertEqual (logging .getLogger ().level , logging .DEBUG )
207218 finally :
208219 # Restore original log level
209- logging .getLogger ().setLevel (original_level )
220+ logging .getLogger ().setLevel (original_level )
0 commit comments