1313# limitations under the License.
1414"""
1515Unit tests for the DCApp class.
16+
17+ NOTE: We import DCApp locally within each test function intentionally.
18+ Importing DCApp at the module level would trigger settings initialization
19+ (and potential .env loading) before the `clean_env` fixture in `conftest.py`
20+ has a chance to isolate the environment. This ensures hermetic tests.
1621"""
1722
1823from unittest .mock import MagicMock , patch
1924
2025import pytest
21- from datacommons_mcp .app import DCApp
2226
2327
2428@pytest .fixture
@@ -48,6 +52,8 @@ def mock_fastmcp():
4852
4953def test_app_initialization_default (mock_settings , mock_fastmcp ): # noqa: ARG001
5054 """Test that DCApp initializes with default instructions."""
55+ from datacommons_mcp .app import DCApp
56+
5157 _ = DCApp ()
5258
5359 # Verify FastMCP was initialized with default instructions
@@ -68,6 +74,8 @@ def test_app_initialization_override(
6874 # Configure settings to use custom dir
6975 mock_settings .return_value .instructions_dir = str (custom_dir )
7076
77+ from datacommons_mcp .app import DCApp
78+
7179 _ = DCApp ()
7280
7381 # Verify FastMCP was initialized with custom instructions
@@ -85,6 +93,8 @@ def test_load_instruction_tool_override(mock_settings, tmp_path, create_test_fil
8593 # Configure settings to use custom dir
8694 mock_settings .return_value .instructions_dir = str (custom_dir )
8795
96+ from datacommons_mcp .app import DCApp
97+
8898 app = DCApp ()
8999 content = app ._load_instruction ("tools/test_tool.md" )
90100 assert content == "Custom Tool Instructions"
@@ -99,6 +109,8 @@ def test_load_instruction_fallback(mock_settings, tmp_path):
99109 # Configure settings to use custom dir
100110 mock_settings .return_value .instructions_dir = str (custom_dir )
101111
112+ from datacommons_mcp .app import DCApp
113+
102114 app = DCApp ()
103115
104116 # Should fall back to default package resource (server.md exists in package)
@@ -112,6 +124,8 @@ def test_register_tool(mock_settings, mock_fastmcp, tmp_path, create_test_file):
112124 create_test_file ("instructions/tools/sample.md" , "Sample Tool Description" )
113125 mock_settings .return_value .instructions_dir = str (tmp_path / "instructions" )
114126
127+ from datacommons_mcp .app import DCApp
128+
115129 app = DCApp ()
116130 mock_mcp_instance = mock_fastmcp .return_value
117131
0 commit comments