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

  • monkeypatch
  • setattr
import app

def test_sum(monkeypatch):
    monkeypatch.setattr(app, 'data_file', 'test_1.json')

    res = app.do_something()    # It is now test_1.json
    ...


def test_again():
    res = app.do_something() # back to the original value
    ...