How do you properly integrate unit tests for file parsing with pytest?
One solution is to define a rootdir
fixture with the path to the test directory, and reference all data files relative to this. This can be done by creating a test/conftest.py
(if not already created) with some code like this:
import os
import pytest
@pytest.fixture
def rootdir():
return os.path.dirname(os.path.abspath(__file__))
Then use os.path.join
in your tests to get absolute paths to test files:
import os
def test_read_favorite_color(rootdir):
test_file = os.path.join(rootdir, 'test_files/favorite_color.csv')
data = read_favorite_color(test_file)
# ...