【问题标题】:PyTest parameterised test with csv data使用 csv 数据的 PyTest 参数化测试
【发布时间】:2023-01-20 20:31:05
【问题描述】:

在许多带有参数化数据的 PyTest 脚本示例中,例如

@pytest.mark.parametrize("input1, input2, output", [(5, 5, 10), (7, 5, 12)])
def test_add(input1, input2, output):
    assert input1 + input2 == output, "failed"

注解。但是,我需要在 PyTest 参数化中为多个 test_ 方法获取 cvs/xlsx 数据。

可以说我在 CSV 中有表格

input1 input2 output
5 5 10
7 5 12

谁能建议从 CSV 读取数据并将其用于上述测试方法的详细解决方案?

【问题讨论】:

    标签: python-3.x pytest


    【解决方案1】:

    您可以尝试使用 pytest-csv-params,您可以使用 pip install pytest-csv-params 安装它

    然后你可以从你的 csv 中导入数据,如下所示

    from pytest_csv_params.decorator import csv_params
    
    @csv_params(
        data_file="/path to csv/addition.csv",
        data_casts={
            "input1": int,
            "input2": int,
            "output": int,
        },
    )
    def test_addition(input1, input2, output):
        assert input1+ input2 == output, "failed"
    

    更多信息可以在这里找到link

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-15
      • 2018-02-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多