Bottom Page

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 ?
Quote
#2
Got it.
conftest.py has to be at the root of the working directory
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Pyautogui script runs fine if split into two parts together it does not Bmart6969 1 101 Oct-07-2019, 10:53 PM
Last Post: Bmart6969
  pytest loop through multiple tests? burvil 0 215 Sep-26-2019, 11:42 PM
Last Post: burvil
  Invalid argument error thrown. pyseeker 4 424 Sep-10-2019, 07:03 PM
Last Post: pyseeker
  scheduled job only runs once Stan2292 4 228 Aug-19-2019, 08:18 AM
Last Post: fishhook
  Pytest with requests a21250450 3 383 Mar-21-2019, 03:24 PM
Last Post: buran
  Test Case Assertion Error moga2003 10 762 Mar-18-2019, 01:06 AM
Last Post: moga2003
  Running pytest gives " 'cannot import name 'session' " error jasonblais 2 682 Oct-10-2018, 05:02 PM
Last Post: jasonblais
  Python script runs on startup but does not register keystrokes. mericanpi 3 722 Sep-07-2018, 02:58 PM
Last Post: mericanpi
  Code runs as pure python but not jupyter notebook miner_tom 3 666 Aug-22-2018, 04:00 PM
Last Post: miner_tom
  Mix-in class tree file not running the self test code. arjunsingh2908 3 648 Aug-14-2018, 05:46 PM
Last Post: arjunsingh2908

Forum Jump:


Users browsing this thread: 1 Guest(s)