'How to run a pytest test function on all data files in a folder

I have a set of functions that I am attempting to write pytest unit tests for. The function I am trying to write a test for looks like so:

def IndexLevel(df: pd.DataFrame, index_row: str, start_col: str, end_col: str) -> pd.DataFrame:

    # Editing df
    return df_index

And the pytest function looks like so (df_format_index is a fixture to test df_index shape):

@pytest.mark.parametrize(
    "df, index_row, start_col, end_col, df_format_index",
    [(function("blah.txt"), "index level", "index level", "Equity 2", df_format_index)],
    indirect=["df_format_index"],
)
def test_IndexLevel(df: pd.DataFrame, index_row: str, start_col: str, end_col: str, df_format_index: pd.DataFrame):
    print("-----test-IndexLevel------")
    assert (IndexLevel(df: pd.DataFrame, index_row, start_col, end_col).shape == df_format_index.shape)

These functions work if I hard code the filename, but I would like to thoroughly test them by running the test on all data files in a folder. I have tried to use the following function, but it did not work:

def pytest_generate_tests(metafunc):
    filelist = glob.glob("Data/*.txt")
    metafunc.parametrize("filename", filelist)

How can I run the test on all files in the data folder without editing the original function?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source