简体   繁体   中英

All my test functions are loading a fixture that is in the conftest.py, even when they don't need it

I have 2 different test files and some fixtures in my conftest.py:

1)"Test_dummy.py" which contains this function:

def test_nothing():
    return 1

2)"Test_file.py". which contains this function:

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" which contains these fixtures:

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

When i run the tests, "Test_dummy.py" also tries to load the "excelvalidation_io" fixture and it generates error:

In test_nothing: function uses no argument 'csvio'

I have tried to place just the fixture inside the "Test_file.py" and the problem is solved, but i read that it's a good practice to locate all the fixtures in the conftest file.

The function pytest_generate_tests is a special function that is always called before executing any test, so in this case you need to check if the metafunc accepts an argument named "csvio" and do nothing otherwise, as in:

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)

Source

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM