user10045873
user10045873

Reputation:

Pytest: fixture with module scope in test file works, but in conftest.py throws error

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'

How can I fix this ?

Upvotes: 1

Views: 2083

Answers (1)

Viral Modi
Viral Modi

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

Related Questions