how to compare: AssertionError: assert <class ‘streamlit.testing.v1.element_tree.ElementList’> == <class ‘streamlit.delta_generator.DeltaGenerator’>
import pandas as pd
import datetime as dt
import pytest
from streamlit.testing.v1 import AppTest
import streamlit as st
@pytest.fixture(scope="module")
def app_test():
return AppTest.from_file(
"../pages/metric_age.py",
)
@pytest.fixture
def mock_data():
today = dt.date.today()
data = {
"Дата": [today - dt.timedelta(days=i) for i in range(30)],
"Значение": [i for i in range(30)],
}
return st.dataframe(data)
def test_tables(app_test, mock_data):
result = app_test.run()
df = result.dataframe
assert type(df) == type(mock_data)
streamlit.delta_generator.DeltaGenerator is the class that streamlit uses to render components, in your case its the st.dataframe.
AppTest.run() runs and returns the app along all of its components.
When you access result.dataframe you are accessing a list of all dataframes of the app. You then need to find the dataframe you wish to view inside the list, yielding a Dataframe object. This is different from what st.dataframe returns, since this is the object created by the script while DeltaGenerator is a link to the object that will be rendered.
If you want to check if your script has the same data as your mock_data function, use the .value attribute of the AppTest Dataframe, like so
import pandas as pd
import datetime as dt
from streamlit.testing.v1 import AppTest
def app_test() -> AppTest:
return AppTest.from_string(
"""
import streamlit as st
import datetime as dt
today = dt.date.today()
data = {
"Дата": [today - dt.timedelta(days=i) for i in range(30)],
"Значение": [i for i in range(30)],
}
st.dataframe(data)
"""
)
def mock_data():
today = dt.date.today()
data = {
"Дата": [today - dt.timedelta(days=i) for i in range(30)],
"Значение": [i for i in range(30)],
}
return data
def test_tables(app_test: AppTest, mock_data):
mock_data = pd.DataFrame(mock_data)
result = app_test.run()
df = result.dataframe[0].value
assert type(df) == type(mock_data)
assert (df == mock_data).all().all()
assert df.equals(mock_data)
test_tables(app_test=app_test(), mock_data=mock_data())
Note that streamlit creates a pandas dataframe when you send any type of data to st.dataframe.
Thanks for stopping by! We use cookies to help us understand how you interact with our website.
By clicking “Accept all”, you consent to our use of cookies. For more information, please see our privacy policy.
Cookie settings
Strictly necessary cookies
These cookies are necessary for the website to function and cannot be switched off. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms.
Performance cookies
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us understand how visitors move around the site and which pages are most frequently visited.
Functional cookies
These cookies are used to record your choices and settings, maintain your preferences over time and recognize you when you return to our website. These cookies help us to personalize our content for you and remember your preferences.
Targeting cookies
These cookies may be deployed to our site by our advertising partners to build a profile of your interest and provide you with content that is relevant to you, including showing you relevant ads on other websites.