Search code examples
pythonpython-3.xpython-moduledirectory-structurepep8

sharing a module between tests and core - appropriate project structure


I am trying to improve the project structure while adding to a code base. I found a sample structure here which looks like this:

README.rst
LICENSE
setup.py
requirements.txt
sample/__init__.py
sample/core.py
sample/helpers.py
docs/conf.py
docs/index.rst
tests/test_basic.py
tests/test_advanced.py

I notice in particular that requirements.txt and setup.py are on a higher level than tests/ and sample/

If I add sample/classes.py you need only write from classes import MyClass in sample/core.py to get it in there. It cannot however so easily be imported into tests/test_basic.py, does not seem like python 'looks around the corner' like that when importing.

In my case, there is also a MANIFEST.in on the same level with requirements.txt and some files which are not really python but just set things up for the platform on which this runs.

If classes.py were on the same level as requirements.txt I think it would be easily importable by everything in tests/ and in sample/ and their subdirectories, but it may need a __init__.py That doesn't feel right somehow.

So where should it go if both tests/ and sample/ need to be able to use it?


Solution

  • Let's make it easy.

    If I understand correctly, the problem is How to import simple module in test. Which means you want to use something like from simple.classes import MyClass.

    That's easy, just add your root path to PYTHONPATH before executing python test/test_basic.py.

    That's also what an IDE does for you when you execute tests through it.