1/ Regarding your first question, based on my own experience, I would structure the project as follow:
project/
app/
class1.py
__init__.py
...
test/
context.py
test_class1.py
__init__.py
...
setup.py
...
Obviously, these test modules must import your packaged module to test
it. You can do this a few ways:
- Expect the package to be installed in site-packages.
- Use a simple (but explicit) path modification to resolve the package properly.
I highly recommend the latter. Requiring a developer to run setup.py
develop to test an actively changing codebase also requires them to
have an isolated environment setup for each instance of the codebase.
To give the individual tests import context, create a tests/context.py
file:
context.py* should bootstrap the testing context as follow:
import sys
import os
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
import app
from app import class1
...
Then in your test_class1.py file, I would import the tested Class as follow:
from unittest import TestCase
from .context import class1 # or from context.class1 import MyClass
class TestClass1(TestCase):
def setUp(self):
self.to_be_tested = class1.MyClass() # or MyClass()
2/ Regarding your second question about __init__.py file being needed or not in python 3.6+, I let you read the following existing answer:
Is __init__.py not required for packages in Python 3?
Possible Interesting references: