Python pytest参数化-将CSV中的行作为测试用例
我必须读取一个CSV文件,对于每一行中的每一个组合,我需要运行一些方法。我希望将每一行视为一个测试用例。是否可以将行作为param-pytest参数化我的测试用例发送?你能告诉我怎么做吗 以下是伪代码:Python pytest参数化-将CSV中的行作为测试用例,python,rows,pytest,parameterized-tests,Python,Rows,Pytest,Parameterized Tests,我必须读取一个CSV文件,对于每一行中的每一个组合,我需要运行一些方法。我希望将每一行视为一个测试用例。是否可以将行作为param-pytest参数化我的测试用例发送?你能告诉我怎么做吗 以下是伪代码: class test_mytest: def test_rows: for row in csvreader: run_method(row) for onecol in row: run_method2(onecol) 我试过阅读p
class test_mytest:
def test_rows:
for row in csvreader:
run_method(row)
for onecol in row:
run_method2(onecol)
我试过阅读pytest
文档,但不清楚
下面是我使用generate_tests hook for row作为参数所做的工作。我想知道如何对internal for loop函数执行相同的操作。这个内部循环也应该作为测试用例收集
def pytest_generate_tests(metafunc):
read_csvrows()
for funcargs in metafunc.cls.params[metafunc.function.__name__]:
# schedule a new test function run with applied **funcargs
metafunc.addcall(funcargs=funcargs)
class TestClass:
params = {
'test_rows': rows,
}
def test_rows:
run_method(row)
for onecol in row:
test_method2(onecol)
现在,我需要为-for循环调用test_method2生成报告(它是csv文件每行中列中元素列表的参数化方法)。Pytest还需要收集这些数据作为测试用例
感谢您的帮助。谢谢您。您可能需要使用
pytest\u generate\u tests()
钩子,如下所述:这允许您读取csv文件并根据其内容参数化所需的测试
更新:
更新后的问题似乎不完全清楚,但我假设您需要在行和列上测试某些内容。这只需要两个测试:
def test_row(row):
assert row # or whatever
def test_column(col):
assert col # or whatever
现在剩下的就是使用pytest\u generate\u tests()
hook为row
和col
创建参数化装置。因此在conftest.py
文件中:
def test_generate_tests(metafunc):
rows = read_csvrows()
if 'row' in metafunc.fixturenames:
metafunc.parametrize('row', rows)
if 'col' in metafunc.fixturenames:
metafunc.parametrize('col', list(itertools.chain(*rows)))
请注意,使用了推荐的
metafunc.parametrize()
函数,而不是不推荐使用的metafunc.addcall()
嘿,感谢您的回复。我之前阅读了该链接,并尝试了pytest\u generate\u tests()钩子。出现了一些问题,但现在已解决。这个链接帮助我现在最了解如何生成报告,我需要使内部for循环看起来也像一个测试用例。有什么想法吗?你能用实际的pytest\u generate\u tests()
代码更新这个问题,并显示一个或两个用它参数化的测试函数吗?否则很难猜出你的意思。嗨,福禄布,我更新了这个问题。你能帮我满足这个要求吗?我试过参数化标记,也试过@params。另一个快速信息是,对于我在python greenlets中使用的第二个函数