Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make python unit tests always find test data files when run from different working directories?

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?

like image 594
ptrico Avatar asked Jan 16 '13 15:01

ptrico


People also ask

Where are Python test files stored?

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.

Should unit tests be in a separate file?

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.

Which function in unit test will run all of your tests in Python?

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.


1 Answers

A number of different ways come to mind:

  1. Set an environment variable for your data directory
  2. Write a small module that you always import that has the sole purpose of having a fixed position relative to your data directory, then call __file__ from there
  3. Generate the data at runtime
  4. Store your data in a database rather than a file
  5. Store your data in a fixed location in the file system rather than a location relative to the package
  6. Don't run your test code directly

The solution that makes the most sense for you depends upon your environment and your specific data and program.

like image 129
Silas Ray Avatar answered Oct 04 '22 05:10

Silas Ray