Product
Introducing License Enforcement in Socket
Ensure open-source compliance with Socket’s License Enforcement Beta. Set up your License Policy and secure your software!
Mock of streamlit to allow unit tests
Our simple streamlit applciation calling a REST API backend grew over time, became not so simple and needed a test suite. We wrote some Selenium tests, but these are tricky to get right and run relatively slowly. This package "mocks" most streamlit class to allow "pytest" to be used for testing.
Goals:
Allow tests to be written using pytest
Tests should run quickly so that multiple edge cases and variants can be tested
Tests should be concise and easy to write
Non-Goals
Testing streamlit itself (the package removes all dependnecies on the real streamlitk)
Testing that the app uses Streamlit correctly (the package fakes input and records outputs)
To be as complete as an end-to-end Selenium test (Streamlit/web server are out of the loop)
The "mock" is probably missing some Streamlit calls.
The "mock" code could probably be simplified using Python magic to remove some of the boilerplate
If you want to mock the values of input elements, they should all have a "key" value. This may require some application changes but it is not obtrusive and is probably good preactice anyway.
The "test" directory contains some very simple Streamlit applications, each with a corresponding test. The tests also serve as programming examples.
$ cd test/simple_calculator
then, to run the application under streamlit:
$ streamlit run main_simple_calculator.py
and to run the test with Streamlit mocked out
$ pytest test_simple_calculator.py
See the examples, but the steps are-.
import streamlit_mock - this adds itself to the front of the Python "path" so that the real Streamlit will not be used.
Create a "test_xxx" function to be executed by "pytest"
Set the inputs in st.session_state
, See the "form_calculator" example to see how form_submit_button's are handled. These do not have a "key".
Run the streamlit application and get the "results".
"results" is a dictionary of lists where the interactions are recorded
Assert that the results are as expected. I have found it useful to temporarily print the results during test development to see what is expected/missing and then add asserts to confirm this behavior on future runs. Perhaps this should be added as a pytest option
Some tests require that the streamlit application be run more than once to create intermediate results in session_state
.
In this case, you can follow the following sps (assuminh two "run"s)
session_state_1
session_state_1
session_state_2
session_state_2.update(session_state_1)
session_state_2
for the second runFAQs
Mocks the stremlit interfaces for unit testing
We found that streamlit-mock demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Ensure open-source compliance with Socket’s License Enforcement Beta. Set up your License Policy and secure your software!
Product
We're launching a new set of license analysis and compliance features for analyzing, managing, and complying with licenses across a range of supported languages and ecosystems.
Product
We're excited to introduce Socket Optimize, a powerful CLI command to secure open source dependencies with tested, optimized package overrides.