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
      • tests
      • 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 needs/uses the file called TEST_DATA.dat

The 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 file

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

Now doing a

  • python build
  • python 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)))

#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