Asked for help on the streamlit forums and got a great answer: https://discuss.streamlit.io/t/mocking-out-a-postgres-connection-in-session-state-with-pytest-monkeypatch/85907/9
The gist is to organize the session into multiple classes instead of a nested one.
class FakeSession:
def __enter__(self):
return self
def __exit__(self, exc_type, exc_value, traceback):
return None
def execute(self, statement):
return [[100]]
def commit(self):
pass
class FakeConnection:
session = FakeSession()
class FakeSessionState:
conn = FakeConnection()
class Test_Burger:
def test_flip_burger(self, monkeypatch):
fake_session_state = FakeSessionState()
monkeypatch.setattr(st, "session_state", fake_session_state)
borger = Burger()
assert borger.flip_burger() == ["100"]