Python Forum
pytest fixture in conftest.py thrown error while in the test file runs
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
pytest fixture in conftest.py thrown error while in the test file runs
#1
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 ?
Reply
#2
Got it.
conftest.py has to be at the root of the working directory
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Pytest Installed, but VS Code Won’t Access Pytest AstralWeeks 9 2,875 Sep-13-2023, 03:00 PM
Last Post: AstralWeeks
  How to do 100 runs simulation based on the current codes? dududada 6 900 Sep-03-2023, 01:43 PM
Last Post: deanhystad
  Pytest mocks anthonyrmoss78 0 426 May-30-2023, 08:28 PM
Last Post: anthonyrmoss78
  unittest generates multiple files for each of my test case, how do I change to 1 file zsousa 0 918 Feb-15-2023, 05:34 PM
Last Post: zsousa
  which exception should be thrown for a sequence of the wrong length? Skaperen 1 826 Jan-06-2023, 04:13 AM
Last Post: deanhystad
  Another program runs bho68 7 1,143 Nov-08-2022, 08:16 PM
Last Post: bho68
  How to sort .csv file test log which item first fail and paint color SamLiu 24 4,700 Sep-03-2022, 07:32 AM
Last Post: Pedroski55
  Importing a function from another file runs the old lines also dedesssse 6 2,478 Jul-06-2021, 07:04 PM
Last Post: deanhystad
  How to test and import a model form computer to test accuracy using Sklearn library Anldra12 6 3,063 Jul-03-2021, 10:07 AM
Last Post: Anldra12
  How to make a test data file for the full length of definition? MDRI 6 3,479 Apr-16-2021, 01:47 AM
Last Post: MDRI

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020