Skip to content

Commit 4315848

Browse files
committed
Add tests for preloadImports
1 parent f8f509d commit 4315848

File tree

1 file changed

+29
-1
lines changed

1 file changed

+29
-1
lines changed

tests/unit/backend/test_main.py

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import pytest
44

5-
from snapred.__main__ import main
5+
from snapred.__main__ import _preloadImports, main
66

77

88
@pytest.mark.parametrize("option", ["-h", "--help", "-v", "--version"])
@@ -35,3 +35,31 @@ def test_configure():
3535
):
3636
main(["--configure"])
3737
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

Comments
 (0)