Python Forum
pytest fixture in conftest.py thrown error while in the test file runs - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: pytest fixture in conftest.py thrown error while in the test file runs (/thread-11907.html)



pytest fixture in conftest.py thrown error while in the test file runs - OzzieOzzum - Jul-31-2018

While Testing a @pytest.fixture(scope="module")

When the fixture is included in the test file - tests/test_authors.py, test works fine

import json, pytest


@pytest.fixture(scope='module')
def author_file_json(tmpdir_factory):
    python_author_data = {
        'Ned': {'City': 'Boston'},
        'Brian': {'City': 'Portland'},
        'Luciano': {'City': 'Sau Paulo'}
    }

    file = tmpdir_factory.mktemp('data').join('author_file.json')
    print('file:{}'.format(str(file)))

    with file.open('w') as f:
        json.dump(python_author_data, f)
    return file


def test_brian_in_portland(author_file_json):
    with author_file_json.open() as f:
        authors = json.load(f)
    assert authors['Brian']['City'] == 'Portland'


If I add the fixture author_file_json to conftest.py and run pytest --fixtures, it shows up in the trace

But now if I run pytest tests/test_authors.py, I get an error -
Error:
E fixture 'author_file_json' not found
How can I fix this ?


RE: pytest fixture in conftest.py thrown error while in the test file runs - OzzieOzzum - Jul-31-2018

Got it.
conftest.py has to be at the root of the working directory