I have a streamlit app, for which I'd like to create unit tests. I am unable to mock objects though.
I have a foo.py with the following content:
import streamlit as st
def dialog_func():
result = dummy_func()
st.write(result)
def dummy_func():
return 'foo'
dialog_func()
and the following corresponding test_foo.py:
from unittest.mock import patch
from streamlit.testing.v1 import AppTest
def test_foo():
with patch("foo.dummy_func") as mock_dummy_func:
mock_dummy_func.return_value ='bar'
at = AppTest.from_file("foo.py").run()
assert at.markdown[0].value == 'bar'
Running this shows, that patching the method does not work and 'foo' is returned instead of 'bar':
$ pytest test_foo.py
...
======================================= short test summary info =======================================
FAILED test_foo.py::test_foo - AssertionError: assert 'foo' == 'bar'
========================================== 1 failed in 0.26s ==========================================