如何 运行 pytest 测试一个文件夹中所有数据文件的函数

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

我有一组函数,我正在尝试为其编写 pytest 单元测试。我尝试为其编写测试的函数如下所示:

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

    # Editing df
    return df_index

pytest 函数看起来像这样(df_format_index 是测试 df_index 形状的夹具):

@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)

如果我对文件名进行硬编码,这些函数就可以工作,但我想通过 运行对文件夹中的所有数据文件进行测试来彻底测试它们。我尝试使用以下功能,但没有用:

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

如何在不编辑原函数的情况下运行对data文件夹下的所有文件进行测试?

这是我最后做的。由于 pytest.mark.parametrize 需要一个元组列表,所以我创建了一个函数 returns 一个元组列表作为测试参数。希望对大家有所帮助!

def IndexLevel(filename: str, index_row: str, start_col: str, end_col: str) -> pd.DataFrame:
    # Editing a df from the file
    return df_index
def file_loop_index() -> list:
    filenames = []
    files = glob.glob("Data/*.txt")
    for file in files:
        filenames.append(tuple((file, "index level", "s&p500", "equity 2", df_format_index)))
    return filenames

# Testing df_index shape against the dummy dataframe
@pytest.mark.parametrize("filename, index_row, start_col, end_col, df_format_index", file_loop_index(), indirect=["df_format_index"])
def test_IndexLevel(
    filename: str,
    index_row: str,
    start_col: str,
    end_col: str,
    df_format_index: pd.DataFrame,
):
    print("-----test-IndexLevel------")
    assert (IndexLevel(filename, index_row, start_col, end_col).shape == df_format_index.shape)