我的conftest.py中有2个不同的测试文件和一些固定装置:
1)“ Test_dummy.py”包含以下功能:
def test_nothing():
return 1
2)“ Test_file.py”.其中包含以下功能:
def test_run(excelvalidation_io):
dfInput, expectedOutput=excelvalidation_io
output=run(dfInput)
for key, df in expectedOutput.items():
expected=df.fillna(0)
real=output[key].fillna(0)
assert expected.equals(real)
3)“ conftest.py”,其中包含以下固定装置:
def pytest_generate_tests(Metafunc):
inputfiles=glob.glob(DATADIR+"**_input.csv", recursive=False)
iofiles=[(ifile, getoutput(ifile)) for ifile in
inputfiles]
Metafunc.parametrize("csvio", iofiles)
@pytest.fixture
def excelvalidation_io(csvio):
dfInput, expectedOutput= csvio
return(dfInput, expectedOutput)
@pytest.fixture
def client():
client = app.test_client()
return client
当我运行测试时,“ Test_dummy.py”也会尝试加载“ excelvalidation_io”固定装置,并且会产生错误:
In test_nothing: function uses no argument 'csvio'
我试图仅将灯具放置在“ Test_file.py”中,问题已解决,但是我读到,将所有灯具放置在conftest文件中是一种很好的做法.
解决方法:
函数pytest_generate_tests是一个特殊函数,在执行任何测试之前总是会调用它,因此在这种情况下,您需要检查Metafunc是否接受名为“ csvio”的参数,否则不执行任何操作,例如:
def pytest_generate_tests(Metafunc):
if "excelvalidation_io" in Metafunc.fixturenames:
inputfiles=glob.glob(DATADIR+"**_input.csv", recursive=False)
iofiles=[(ifile, getoutput(ifile)) for ifile in
inputfiles]
Metafunc.parametrize("csvio", iofiles)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。