使用动态列表的 Pytest 参数化

Pytest Parametrization using a dynamic list

我正在写一个pytest,它接受下面的两个输入@

我有以下函数,它获取从 CSV 中读取的数据,并根据传递的客户 ID 执行操作。这里的顾客数量未知。我如何在 pytest 中处理这个问题?

@pytest.mark.parametrize("input_dict, customer", [(input_dict, customer)])
def test_input_values(datadictionary_from_excel, customer_id):
    perform some action based on customer_id which keeps changing and the input_dict which is constant
    assert above action is succesfull 

谢谢

您可以创建一个从 csv 文件读取并创建客户列表的函数,然后使用 @pytest.mark.parametrize

将值传递给测试
def data_source():
    input_dict = read_from_csv()
    customers = get_customers() # ["C1", "C2",.., Cn]
    for customer in customers:
        yield input_dict, customer


@pytest.mark.parametrize("input_dict, customer", data_source())
def test_input_values(input_dict, customer):
    # input_dict is the all data from the csv
    # customer is one item from the customers list, C2 for example