Python version: 3.11
Streamlit Version: 1.30.0
Running it locally
Hi,
We have started covering our application with tests and we have a problem. We would like our tests to be structured as follows.
βββ tests
β βββ unit
β β βββ test_interface.py
β βββ integration
#test_interface.py
from streamlit.testing.v1 import AppTest
def prepare_view():
import streamlit as st
st.multiselect(label='test', options=['foo', 'bar'], key='key', default='foo')
class TestView:
def test_change_value(self):
app_test = AppTest.from_function(prepare_view)
app_test.run()
assert app_test.session_state['key'] == ['foo']
app_test.multiselect[0].set_value(['bar']).run()
assert app_test.session_state['key'] == ['bar']
With this structure, running a test with the command pytest tests/unit/test_interface.py
works correctly
An error occurs when running all the tests of the project with the command pytest tests
Exception:
self = SessionState(_new_session_state={'$$STREAMLIT_INTERNAL_KEY_SCRIPT_RUN_WITHOUT_ERRORS': True}, _new_widget_state=WStates()), key = 'key'
...
E KeyError: 'st.session_state has no key "key". Did you forget to initialize it? More info: https://docs.streamlit.io/library/advanced-features/session-state#initialization'
key = 'key'
self = SessionState(_new_session_state={'$$STREAMLIT_INTERNAL_KEY_SCRIPT_RUN_WITHOUT_ERRORS': True}, _new_widget_state=WStates())
wid_key_map = {}
widget_id = 'key'
If we move the file with this test to the tests/ folder and run it with the pytest tests
command, everything will work correctly
Can you please help how to make the tests work correctly in the unit subfolder when running all tests?