4

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.

  • But to be loaded at every test is what `conftest.py` is for, isn't it? Move your fixtures elsewhere. – ivan_pozdeev Apr 24 '19 at 17:09
  • Possible duplicate of [In pytest, what is the use of conftest.py files?](https://stackoverflow.com/questions/34466027/in-pytest-what-is-the-use-of-conftest-py-files) – ivan_pozdeev Apr 24 '19 at 17:11

1 Answers1

3

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

sanyassh
  • 8,100
  • 13
  • 36
  • 70
Ivan Castellanos
  • 8,041
  • 1
  • 47
  • 42