|
2 | 2 |
|
3 | 3 | import pytest
|
4 | 4 |
|
5 |
| -from snapred.__main__ import main |
| 5 | +from snapred.__main__ import _preloadImports, main |
6 | 6 |
|
7 | 7 |
|
8 | 8 | @pytest.mark.parametrize("option", ["-h", "--help", "-v", "--version"])
|
@@ -35,3 +35,31 @@ def test_configure():
|
35 | 35 | ):
|
36 | 36 | main(["--configure"])
|
37 | 37 | mockConfigure.assert_called_once()
|
| 38 | + |
| 39 | + |
| 40 | +def test_preloadImports_success(capsys): |
| 41 | + """Test _preloadImports function executes successfully and prints success message.""" |
| 42 | + _preloadImports() |
| 43 | + captured = capsys.readouterr() |
| 44 | + assert "preloaded qtpy.QtWebEngineWidgets" in captured.out |
| 45 | + assert "SNAPRed algorithms and services registered with Mantid" in captured.out |
| 46 | + |
| 47 | + |
| 48 | +def test_preloadImports_exception_handling(capsys): # noqa: ARG001 |
| 49 | + """Test _preloadImports function handles exceptions properly and prints warning.""" |
| 50 | + # Mock an import that will raise an exception during the try block |
| 51 | + with mock.patch("snapred.__main__.print") as mock_print: |
| 52 | + # Make the first print call succeed (for the qtpy import message) |
| 53 | + # But make the second print call (success message) raise an exception |
| 54 | + # Add a third None for the warning message call |
| 55 | + mock_print.side_effect = [None, Exception("Test exception"), None] |
| 56 | + |
| 57 | + _preloadImports() |
| 58 | + |
| 59 | + # Verify the exception handling code was called |
| 60 | + expected_calls = [ |
| 61 | + mock.call("preloaded qtpy.QtWebEngineWidgets"), |
| 62 | + mock.call("SNAPRed algorithms and services registered with Mantid"), |
| 63 | + mock.call("Warning: Failed to register SNAPRed with Mantid: Test exception"), |
| 64 | + ] |
| 65 | + mock_print.assert_has_calls(expected_calls) |
0 commit comments