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 ?

2
  • If the src folder a package? Said differently does it contain a __init__.py file? Commented Mar 1, 2021 at 13:26
  • Yes. It is a package. So I am not sure whether or not to install it each time I want to debug which would be painful. Commented Mar 1, 2021 at 13:35

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks. How do you import classes and stuff from the project ?
If you have the file src/controller.py you can import it (after the sys.path statement) with from controller import *
Shouldn't it be sys.path.append("..//src")
Since I said "Finally, I execute the tests from the project directory", the working dir is project and therefore ./src is correct.
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.