We generally preach that you should not use real data in your unit tests. You should instead mock said data out in an effort to lesson potential side-effects, including degrading test performance and adversely affecting live data.
Sometimes, you’d like to test actual file handling in a unit test. If you do, you should provide a sample data set packaged with your code in a data directory within your test directory.
Your package would then resemble the following structure:
/package /tests /data data.txt __init__.py test_module.py __init__.py module.py
In your test, you’ll need to provide a relative path to the data file. To do this, use the special __file__
attribute of the package to provide a starting point. This will let you know where the module lives on disk regardless of where the package is installed.
import os from package.tests import __file__ as test_directory def _data_dir(): return os.path.join(os.path.dirname(test_directory), 'data') def test_sample_data(): file_path = os.path.join(_data_dir(), 'data.txt') with open(file_path) as f: package.module.do_something(f)
There you have it!