This is a question about testing environment setup.
In my project, I have a few unit tests that access test data files. These unit tests can be run from my project directory via a test runner. Or I can run each test file/module individually, for debugging purposes, for instance.
The problem is that depending on where I run the tests from, the current directory is different. So opening a test data file, as below, by giving a path relative to the current directory will not work when those files are run from the project directory, as the test data file is not in that directory.
f = open('test_data.ext', 'r')
I thought of using __file__ to use a path relative the current test module, but this doesn't work when the test module calling __file__ is the one being run individually.
How do people generally solve this problem?
Tests are put in files of the form test_*. py or *_test.py , and are usually placed in a directory called tests/ in a package's root.
We have a requirement that the unit testing file need to be separate with the source file in project building. It means we must not do this for testing the source file.
TestCase is used to create test cases by subclassing it. The last block of the code at the bottom allows us to run all the tests just by running the file.
A number of different ways come to mind:
__file__
from thereThe solution that makes the most sense for you depends upon your environment and your specific data and program.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With