在 Pytest 中使用 excel 参数化测试

Parameterize tests using excel in Pytest

我是 Python 的新手,在 Pytest 中写过一些测试。我现在想使用 excel 或 csv sheet 参数化我的测试,以将数据传递给测试方法。

我找不到任何帮助。你能帮我解决一下吗?或者参考任何 link.

谢谢 苏尼尔

大体思路应该是使用parametrized test

例如,您可以添加一个带有输入文件名的输入参数

https://docs.pytest.org/en/latest/parametrize.html

尝试编写一个函数来从 excel 或 CSV 中检索 "data" 的列表。然后你可以像这样使用参数化函数:

def get_data():
    # Retrieve values from CSV
    return ['value1', 'value2', 'value3']

@pytest.mark.parametrize("value", get_data())
def test_sample(self, value):
    print(value)

此示例将生成 3 个测试,将从 get_data() 返回的每个元素作为值传递。

注意:查看 CSV 文档 https://docs.python.org/3.6/library/csv.html 以获取有关如何打开 CSV 文件并将值转换为列表的帮助。