Skip to content

Instantly share code, notes, and snippets.

@tasdikrahman
Last active June 30, 2024 00:15
Show Gist options
  • Save tasdikrahman/2bdb3fb31136a3768fac to your computer and use it in GitHub Desktop.
Save tasdikrahman/2bdb3fb31136a3768fac to your computer and use it in GitHub Desktop.
Typical Directory structure for python tests

A Typical directory structure for running tests using unittest

Ref : stackoverflow

The best solution in my opinion is to use the unittest command line interface which will add the directory to the sys.path so you don't have to (done in the TestLoader class).

For example for a directory structure like this:

new_project
├── antigravity.py
└── test_antigravity.py

You can just run:

$ cd new_project
$ python -m unittest test_antigravity

For a directory structure like yours:

new_project
├── antigravity
│   ├── __init__.py         # make it a package
│   └── antigravity.py
└── test
    ├── __init__.py         # also make test a package
    └── test_antigravity.py

And in the test modules inside the test package, you can import the antigravity package and its modules as usual:

# import the package
import antigravity

# import the antigravity module
from antigravity import antigravity

# or an object inside the antigravity module
from antigravity.antigravity import my_object

Running a single test module:

To run a single test module, in this case test_antigravity.py:

$ cd new_project
$ python -m unittest test.test_antigravity

Just reference the test module the same way you import it.

Running a single test case or test method:

Also you can run a single TestCase or a single test method:

$ python -m unittest test.test_antigravity.GravityTestCase
$ python -m unittest test.test_antigravity.GravityTestCase.test_method

Running all tests:

You can also use test discovery which will discover and run all the tests for you, they must be modules or packages named test*.py (can be changed with the -p, --pattern flag):

$ cd new_project
$ python -m unittest discover

This will run all the test*.py modules inside the test package.

@samyouaret
Copy link

You need to add __init__.py to the tests directory, If you have other subdirectories in the tests directory you also need to add the __init__.py so python can know that those are modules, not just directories.

then run:

python3 -m unittest discover -p 'test_*.py'

@10cheon00
Copy link

10cheon00 commented Sep 26, 2022

Adding __init__.py into tests/APPS_NAME/ is working! 👍

my test folder structure

root
+-- config
+-- mysite
|   `-- models, views, urls...
`-- tests
    `-- mysite
        +-- __init__.py
        +-- tests_models.py
        `-- other tests...

Thanks @samyouaret

@samyouaret
Copy link

you are welcome @10cheon00 .

@mrdougwright
Copy link

This was so very helpful. Thank you. 🎉

@paulied67
Copy link

This is great,

Question, do you have an example such that given this directory structure :

image

I want to know how to import src > mypkg > app.py into tests > unit> app_test.py

What if, in this example, app.py contains the following import: import view? I find that the test fails trying to import view.py:

ModuleNotFoundError: No module named 'view'

It's almost as if the directory the test is run in becomes the "current relative" directory and, because app.py is being imported from a test, ie app_test.py, that the imports are expected from the test's location and not the location of the file importing the module.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment