20

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?

Community
  • 1
  • 1
ptrico
  • 1,049
  • 7
  • 22
  • I realise this question is not purely python specific, but I would favour python idiomatic solutions, if there are any. – ptrico Jan 18 '13 at 03:07

2 Answers2

11

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.

Silas Ray
  • 25,682
  • 5
  • 48
  • 63
  • Thanks for the various options. I ended going for #2, after initially discarding because I couldn't make it work. It's all good now. – ptrico Jan 18 '13 at 03:09
  • I used #2 as well, with this approach: http://stackoverflow.com/a/5748583/1082367 - os.path.abspath(inspect.getsourcefile(local_function)) – Matthew Cornell Apr 23 '13 at 20:44
-1

I'm not sure it's your complete solution, but for Unit testing in Python, I always use Nose. It has an excellent Test Discovery procedure . You can find its mechanism here. Maybe you can get some idea to use for Python's traditional unit testing as well ..

py.test also uses a neat discovering mechanism ..

Vahid Rafiei
  • 382
  • 2
  • 10
  • 1
    +1 for suggesting using a unit testing framework for running tests. Doesn't necessarily solve the external data file problem, but definitely a good idea none the less. – Silas Ray Jan 16 '13 at 15:29
  • That's right, and the test runner I mentioned in the question is in fact nose. – ptrico Jan 18 '13 at 03:08