Debian Codegraph @

tests/test_coverup_71.py

raw symbols SCIP
1 # file scalene/__main__.py:13-21
2 # lines [13, 14, 15, 17, 18, 19, 20, 21]
3 # branches []
4
5 import pytest
6 from unittest.mock import patch
7 import sys
8 import io
9 from scalene import scalene_profiler
10
11
12 # Test function to improve coverage for the main function in scalene.__main__
13 def test_main_exception_handling():
14 # Mock the Scalene main function to raise an exception
15 with patch(
16 "scalene.scalene_profiler.Scalene.main", side_effect=Exception("Test Exception")
17 ):
18 # Redirect stderr to capture the output
19 with patch("sys.stderr", new=io.StringIO()) as fake_stderr:
20 # Mock sys.exit to prevent the test from exiting
21 with patch("sys.exit", side_effect=SystemExit) as mock_exit:
22 # Call the main function which should now raise an exception
23 with pytest.raises(SystemExit):
24 from scalene.__main__ import main
25
26 main()
27 # Check that the exception message was printed to stderr
28 assert (
29 "ERROR: Calling Scalene main function failed: Test Exception"
30 in fake_stderr.getvalue()
31 )
32 # Check that sys.exit was called with the correct exit code
33 mock_exit.assert_called_once_with(1)