Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Pytest: Hard-coded path - monkeypatch attribute - tmpdir

An even better approach might be to use the tmpdir fixture to create a temporary folder, create the necessary file(s) there and use the monkeypatch fixture to point there.

import app

def test_app_1(monkeypatch, tmpdir):
    mocked_data_file = tmpdir.join('test_1.json')
    monkeypatch.setattr(app, 'data_file', mocked_data_file)

    res = app.do_something()
    ...

def test_app_2():
    res = app.do_something()    # back to the original value
    ...
/tmp/pytest-of-gabor/pytest-2/test_sum0/test_1.json
./corporate/fixed/path/data.json
.
2 passed in 0.04s