Manually Patching attribute - Fixtures and Mocking in Python
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
Manually Patching attribute
We can replace the attribute during the test run and create a json file locally to be used. At least two problems with this:
- Readers might glance over the assignment and might be baffled
- The change is permanent in the whole test script so one test impacts the other.
import app
def test_sum():
app.data_file = 'test_1.json' # manually overwrite
res = app.get_sum()
assert True
assert res == 42
def test_again():
print(app.data_file) # it is still test_1.json
{
"x": 19,
"y": 23
}