When code under test uses shared mutable state (module globals, singletons, tool registrations) or introduces new public behaviors, unit tests must (1) assert meaningful outcomes/calls and (2) isolate themselves by resetting/restoring that shared state.
Practical rules:
autouse fixture to snapshot/restore state before/after each test (or ensure the production code is idempotent).__version__) or computed expectations.Example pattern for isolation:
import pytest
# Suppose module has a singleton/global that tests mutate/consume
import my_module
@pytest.fixture(autouse=True)
def isolate_module_state():
snapshot = dict(my_module.SINGLETON.__dict__) # or snapshot the singleton object reference
yield
my_module.SINGLETON.__dict__.update(snapshot)
# or my_module.reset_singleton()
Example pattern for meaningful assertions:
async def test_startup_calls_connection_factory(mocker, monkeypatch):
monkeypatch.setattr(sys, "argv", ["prog", "--region", "us-east-1", "--db_endpoint", "host"])
internal = mocker.patch.object(server, "internal_create_connection", return_value=(object(), {"status": "Connected"}))
mcp_run = mocker.patch.object(server.mcp, "run")
server.main()
internal.assert_called_once() # not optional
assert mcp_run.called
Applying these standards prevents flaky/cross-test failures, makes regressions detectable, and ensures new behavior is actually covered.