My project hierarchy goes like this
project
src
fruit_pkg
count_fruits.py
test
fruit_pkg
test_count_fruits.py
Now say inside the module count_fruits.py I have a function called addition. In order to test addition, do I have to import the count_fruits module using
my_mod = __import__("fruit_pkg.count_fruits")
cnt_fruit = getattr(my_mod,"count_fruits")
and then for my test do something like
def my_test(self):
#some work happen here
cnt_fruit.addition(blah, blah)
#more work happen here
Or: since both the src and the test codes are in the same package fruit_pkg, is there a simpler way to access the functions inside count_fruits.
For parallel, in Java, if both the src and the junit test codes are inside the same package, no import is necessary at all.
Unless
fruit_pkgis a namespace package; it is available only from one directory. Look atsys.modules['fruit_pkg']to find out which one.You could move tests to
src/fruit_pkg/test/test_count_fruits.pyor don’t use the same top-level name for testing packages.Either way, to import the module: