How to compare dataframes in tests

  1. Local deploy
  2. 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.

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