[英]All my test functions are loading a fixture that is in the conftest.py, even when they don't need it
我的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)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.