I am trying to get my unit test scripts to work no matter where they are invoked from and have looked into a dozen questions on relative import issues now. But relative paths in combination with the unittest module seem to add some extra complications.
My directory structure is this:
importtest
├── importtest
│ ├── importtest.py
│ └── __init__.py
└── test
├── data.txt
├── importtest_test.py
└── __init__.py
the __init__.py
files are empty, importtest.py
contains
def func(filename):
with open(filename) as f:
return True
and importtest_test.py
contains
import unittest
from importtest import importtest
class Test(unittest.TestCase):
def test(self):
self.assertEqual(importtest.func('data.txt'), True)
This works when I execute py -m unittest test/importtest_test.py
from the root folder of the project (the top most importtest
folder). But it does not work from any other directory.
Why is that and how can I fix this?