[TIP] I have some data files I want to use in my tests -- how do I specify where they are?

David Stanek dstanek at dstanek.com
Sun Dec 20 09:12:12 PST 2009

On Sun, Dec 20, 2009 at 11:43 AM, Matthew Wilson <matt at tplus1.com> wrote:
> I'm writing some code to extract and yield some attachments out of
> email messages.  I have a file named email_parsers.py and another file
> in a tests/ subdirectory named test_email_parsers.py
> I have a few email messages saved as text files. These files are in
> that tests/ directory.
> In my test code, I want to know how to open those email files.  Should
> I use something like os.dirname(__file__)?
> I know I won't always run my tests from the tests/ subdirectory

I always do something like:

  MYDIR = os.path.abspath(os.path.dirname(__file__))
  DATADIR = os.path.join(MYDIR, '../test-data')
  CONFDIR = os.path.join(MYDIR, '../config')

blog: http://www.traceback.org
twitter: http://twitter.com/dstanek

More information about the testing-in-python mailing list