我的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)