python package install which directory is the test data in

I am moving to using CI development process as shown in the rough sketch below

  • Proj
    • Message
    • init.py
    • Message.py
      • tests
      • init.py
      • test_msg.py
      • TEST_DATA.dat

I hope that has clearly shown that I am, Creating a Package called Message and that I have bundled tests in this package. You should assume that the test_msg.py needs/uses the file called TEST_DATA.dat

Setup.py

The Setup.py would look something like this

setup (name='Proj',
       packages=['Message'. 'Message.test'],
       .
       .
       .)

This will not pull the TEST_DATA.dat, what you need to do is to create a MANIFEST.in file

include requirements.txt
include Message/tests/TEST_DATA.dat

Now doing a

  • python setup.py build
  • python setup.py sdist

Will include these file in the build package.

My Test Does not work

Your test file looks like this

def test_001_open_file():
    open("TEST_DATA.dat","rt") as test_file:
    assert True

Whilst this may not be the greatest tests ever, it will fail - with

File Not Found

We have no idea where it has been installed, and where we are..... So how to correct that ?

from inspect import getsourcefile
from os import chdir
from os.path import abspath, dirname

where_are_we = dirname(abspath(getsourcefile(lambda:0)))
chdir(where_are_we)

#Now  open will work
#As we have switched to the directory where the test_file and the TEST_DATA.dat file are located. 
#You can of course just prepend the path to the file also.
#

with open("TEST_DATA.dat","rt") as test_file:
    assert True