Headless testing with AppTest and st.form

Greetings, is it possible in the current release of streamlit to use AppTest with st.form? If so, what is the syntax for accessing elements in the form? In the documentation, I see references to st.form_submit_button but not st.form itself.

Answering my own questions here: as of streamlit 1.28.0 running AppTest is basically agnostic as to whether fields are in an st.form, so I didn’t have to do anything extra.

For example, the following should behave the same with testing:

# with form
with st.form("Some form", key="my_form"):
    num = st.number_input("Select a number",0,value=10000,key="my_num")
    if st.form_submit_button("Check number",key="my_button"):
        st.write(num)
# without form
num = st.number_input("Select a number",0,value=10000,key="num")
if st.button("Check number",key="my_button"):
        st.write(num)

Which will simply require:

from streamlit.testing.v1 import AppTest 
at = AppTest.from_file("path/to/script.py")
test_num = 2
at.run()
at.number_input(f"my_num").set_value(test_num)
at.button[0].click()
at.run()

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.