Hi all!!!
I did a search about how to test a component, but the solutions were too much.
I was looking for something easy and fast.
After some thinking I came up with this solution, small and fast tests for each component:
from streamlit.testing.v1 import AppTest
class TestTitleComponent:
def test_title(self) -> None:
app = AppTest.from_file("src/delivery/streamlit/components/title.py")
at = app.run()
assert at.title[0].value == "Hello, world!"
In the component I use the if __name__ == "__main__":
to render the component when I used it alone:
import streamlit as st
class Title:
def render(self) -> None:
st.title("Hello, world!")
if __name__ == "__main__":
title = Title()
title.render()
That way the component could be tested in isolation, but also you can test the complete app:
from streamlit.testing.v1 import AppTest
class TestStreamlitApp:
def test_hello_world(self) -> None:
at = AppTest.from_file("src/delivery/streamlit/app.py").run()
assert at.title[0].value == "Hello, world!"
assert at.title[1].value == "Bye, bye!"
from src.delivery.streamlit.components.title import Title
title = Title()
title.render("Hello, world!")
title.render("Bye, bye!")
What do you think? Am I missing something here? Do you like it?