Hard coded path - 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
Hard coded path
In many application we can find hard-coded pathes. In order to test them we will need to create that exact path which is not always easy. This also means we cannot run two tests at the same time with different content in those files. (The actual “application” is just adding numbers together.)
import json
data_file = "/corporate/fixed/path/data.json"
def get_sum():
with open(data_file) as fh:
data = json.load(fh)
# ...
result = data['x'] + data['y']
return result
import app
def test_sum():
res = app.get_sum()
assert True
pytest test_data_1.py
def get_sum():
> with open(data_file) as fh:
E FileNotFoundError: [Errno 2] No such file or directory: '/corporate/fixed/path/data.json'