Reputation:
While Testing a @pytest.fixture(scope="module")
in a project of structure
├── main.py
├── pytest.ini
├── src
│ └── tasks
│ ├── __init__.py
│ └── conftest.py
└── tests
└── func
└── test_authors.py
pytest.ini
contains
[pytest]
xfail_strict=true
When the fixture is included in the test file - tests/func/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'
author_file_json
to conftest.py
pytest --fixtures
pytest tests/test_authors.py
E fixture 'author_file_json' not found
How can I fix this ?
Upvotes: 1
Views: 2083
Reputation: 2155
Put conftest.py
file in a location which is at the same level as your test files directory (func
) or one of the parent directories of your test case files in your project workspace (tests
or root dir of your project
in your case). You currently have it in the sibling directory tasks
which pytest will not scan.
Upvotes: 1