1

Within a python package, what is the generally accept workflow for debugging and testing ?

Currently I have a test folder that sits at the same level as the project src folder. I want the test files (unit tests and others) to call the latest developed Python code in the project in the same-level folder. However for this I need to do some things such as adding folders to the path and relative imports to get it to work. But this seems to break for other project developers.

The alternative is to install the code locally using pip install. But this is a hassle to do every time I want to test.

Can someone please recommend a workflow that is safe and efficient for testing within a project. If the first of these is preferred, what should I do regarding imports and path to get the test code to call the local project code in a reliable way that will work for everyone ?

Dom
  • 160
  • 11

2 Answers2

1

However for this I need to do some things such as adding folders to the path and relative imports to get it to work. But this seems to break for other project developers.

Doing the following, does not break the project AFAIK.

For me the following layout works (see also here):

project
  |--src    # directory for all of your code
  |--test   # directory for tests
  ...

Then I have the following line of code (before importing from src) in each test .py file:

import sys
sys.path.append("./src")

Finally, I execute the tests from the project directory.


EDIT

When using pytest, you can actually move the path-append-statement into your conftest.py file. That way you don't have to add it to each test .py file.

Rafael-WO
  • 1,434
  • 14
  • 24
  • Thanks. How do you import classes and stuff from the project ? – Dom Mar 01 '21 at 10:54
  • If you have the file `src/controller.py` you can import it (after the `sys.path` statement) with `from controller import *` – Rafael-WO Mar 01 '21 at 11:07
  • Shouldn't it be sys.path.append("..//src") – Dom Mar 01 '21 at 21:31
  • Since I said "_Finally, I execute the tests from the **project directory**_", the working dir is `project` and therefore `./src` is correct. – Rafael-WO Mar 02 '21 at 07:18
0

A possible trick is to make the test folder a package by adding an __init__.py file into it:

import sys
import os.path
sys.path.append(os.path.join(os.path.dirname(os.path.dirname(__file__)), 'src')

then assuming the src folder contains a a.py file, you can import it into the test_a.py file from test as simply as import a provided you start the test from the folder containing test as (assuming unittest):

python -m unittest test.test_a

The good news if you follow that pattern is that you can run the full list of tests with

python -m unittest discover

because the magic of discover will find the test package and all test*.py files under it.

Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252