By Major Major

2009-12-13 16:10:23 8 Comments

The very common directory structure for even a simple Python module seems to be to separate the unit tests into their own test directory:


for example see this Python project howto.

My question is simply What's the usual way of actually running the tests? I suspect this is obvious to everyone except me, but you can't just run python from the test directory as its import antigravity will fail as the module is not on the path.

I know I could modify PYTHONPATH and other search path related tricks, but I can't believe that's the simplest way - it's fine if you're the developer but not realistic to expect your users to use if they just want to check the tests are passing.

The other alternative is just to copy the test file into the other directory, but it seems a bit dumb and misses the point of having them in a separate directory to start with.

So, if you had just downloaded the source to my new project how would you run the unit tests? I'd prefer an answer that would let me say to my users: "To run the unit tests do X."


@squid 2014-03-07 07:51:46

You should really use the pip tool.

Use pip install -e . to install your package in development mode. This is a very good practice, recommended by pytest (see their good practices documentation, where you can also find two project layouts to follow).

@aliopi 2017-07-26 21:38:47

Why downvote this answer? I read the accepted answer and while it was not bad, pytest is way better to run tests, because of the console output you get, in color, with stack trace info and detailed assertion error information.

@Carl Meyer 2009-12-13 20:40:58

The simplest solution for your users is to provide an executable script ( or some such) which bootstraps the necessary test environment, including, if needed, adding your root project directory to sys.path temporarily. This doesn't require users to set environment variables, something like this works fine in a bootstrap script:

import sys, os

sys.path.insert(0, os.path.dirname(__file__))

Then your instructions to your users can be as simple as "python".

Of course, if the path you need really is os.path.dirname(__file__), then you don't need to add it to sys.path at all; Python always puts the directory of the currently running script at the beginning of sys.path, so depending on your directory structure, just locating your at the right place might be all that's needed.

Also, the unittest module in Python 2.7+ (which is backported as unittest2 for Python 2.6 and earlier) now has test discovery built-in, so nose is no longer necessary if you want automated test discovery: your user instructions can be as simple as python -m unittest discover.

@Frederic Bazin 2012-05-23 16:07:23

I put some tests in a subfolder like as "Major Major". They can run with python -m unittest discover but how can I select to run only one of them. If I run python -m unittest tests/testxxxxx then it fails for path issue. Since dicovery mode solve everything I would expect that there is another trick to solve path issue without handcoding path fix you suggest in first point

@Carl Meyer 2014-07-15 01:01:45

@FredericBazin Don't use discovery if you only want a single test or test file, just name the module you want to run. If you name it as a module dotted-path (rather than a file path) it can figure out the search path correctly. See Peter's answer for more details.

@ixe013 2019-04-23 11:46:46

This hack was usefull in a scenario where I had to run something like python -m pdb tests\ Inside pdb, I executed sys.path.insert(0, "antigravity") which allowed the import statement to resolve as if I was running the module.

@Tom Willis 2009-12-13 16:27:00

if you run "python develop" then the package will be in the path. But you may not want to do that because you could infect your system python installation, which is why tools like virtualenv and buildout exist.

@chasmani 2019-06-26 15:25:38

This way will let you run the test scripts from wherever you want without messing around with system variables from the command line.

This adds the main project folder to the python path, with the location found relative to the script itself, not relative to the current working directory.

import sys, os

sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.realpath(__file__))))

Add that to the top of all your test scripts. That will add the main project folder to the system path, so any module imports that work from there will now work. And it doesn't matter where you run the tests from.

You can obviously change the project_path_hack file to match your main project folder location.

@pj.dewitte 2019-02-01 09:57:18

If you are looking for a command line-only solution:

Based on the following directory structure (generalized with a dedicated source directory):


Windows: (in new_project)

$ python -m unittest discover -s test

See this question if you want to use this in a batch for-loop.

Linux: (in new_project)

$ export PYTHONPATH=$PYTHONPATH:$(pwd)/src  [I think - please edit this answer if you are a Linux user and you know this]
$ python -m unittest discover -s test

With this approach, it is also possible to add more directories to the PYTHONPATH if necessary.

@Mark Byers 2009-12-13 16:25:43

From the article you linked to:

Create a file and put your unittest tests in it. Since the test modules are in a separate directory from your code, you may need to add your module’s parent directory to your PYTHONPATH in order to run them:

$ cd /path/to/googlemaps

$ export PYTHONPATH=$PYTHONPATH:/path/to/googlemaps/googlemaps

$ python test/

Finally, there is one more popular unit testing framework for Python (it’s that important!), nose. nose helps simplify and extend the builtin unittest framework (it can, for example, automagically find your test code and setup your PYTHONPATH for you), but it is not included with the standard Python distribution.

Perhaps you should look at nose as it suggests?

@Major Major 2009-12-13 16:39:31

Yes this works (for me), but I'm really asking for the simplest instructions that I can give users to my module to get them to run the tests. Modifying the path might actually be it, but I'm fishing for something more straight-forward.

@jeremyjjbrown 2014-06-21 23:16:56

So what does your python path look like after you've worked on a hundred projects? Am I supposed to manually go in and clean up my path? If so this is an odious design!

@Qlimax 2018-10-30 15:31:50

If you have multiple directories in your test directory, then you have to add to each directory an file.

└── test
    └── frontend
    └── backend

Then to run every test at once, run:

python -m unittest discover -s /home/johndoe/snakeoil/test -t /home/johndoe/snakeoil

Source: python -m unittest -h

  -s START, --start-directory START
                        Directory to start discovery ('.' default)
  -t TOP, --top-level-directory TOP
                        Top level directory of project (defaults to start

@tjk 2018-10-20 16:25:34

You can't import from the parent directory without some voodoo. Here's yet another way that works with at least Python 3.6.

First, have a file test/ with the following content:

import sys
import os
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))

Then have the following import in the file test/

import unittest
    import context
except ModuleNotFoundError:
    import test.context    
import antigravity

Note that the reason for this try-except clause is that

  • import test.context fails when run with "python" and
  • import context fails when run with "python -m unittest" from the new_project directory.

With this trickery they both work.

Now you can run all the test files within test directory with:

$ pwd
$ python -m unittest

or run an individual test file with:

$ cd test
$ python test_antigravity

Ok, it's not much prettier than having the content of within, but maybe a little. Suggestions are welcome.

@eusoubrasileiro 2018-09-28 15:21:33

Python 3+

Adding to @Pierre

Using unittest directory structure like this:

├── antigravity
│   ├──         # make it a package
│   └──
└── test
    ├──         # also make test a package

To run the test module

$ cd new_project
$ python -m unittest test.test_antigravity

Or a single TestCase

$ python -m unittest test.test_antigravity.GravityTestCase

Mandatory don't forget the even if empty otherwise will not work.

@andpei 2013-12-04 09:45:09

I had the same problem, with a separate unit tests folder. From the mentioned suggestions I add the absolute source path to sys.path.

The benefit of the following solution is, that one can run the file test/ without changing at first into the test-directory:

import sys, os
testdir = os.path.dirname(__file__)
srcdir = '../antigravity'
sys.path.insert(0, os.path.abspath(os.path.join(testdir, srcdir)))

import antigravity
import unittest

@John Greene 2018-08-13 00:50:04

This BASH script will execute the python unittest test directory from anywhere in the file system, no matter what working directory you are in.

This is useful when staying in the ./src or ./example working directory and you need a quick unit test:


dirname="`dirname $this_program`"
readlink="`readlink -e $dirname`"

python -m unittest discover -s "$readlink"/test -v

No need for a test/ file to burden your package/memory-overhead during production.

@kenorb 2015-06-07 11:45:51

It's possible to use wrapper which runs selected or all tests.

For instance:

./run_tests antigravity/*.py

or to run all tests recursively use globbing (tests/**/*.py) (enable by shopt -s globstar).

The wrapper can basically use argparse to parse the arguments like:

parser = argparse.ArgumentParser()
parser.add_argument('files', nargs='*')

Then load all the tests:

for filename in args.files:

then add them into your test suite (using inspect):

alltests = unittest.TestSuite()
for name, obj in inspect.getmembers(sys.modules[__name__]):
    if inspect.isclass(obj) and name.startswith("FooTest"):

and run them:

result = unittest.TextTestRunner(verbosity=2).run(alltests)

Check this example for more details.

See also: How to run all Python unit tests in a directory?

@Alan L 2017-08-07 23:13:39

I noticed that if you run the unittest command line interface from your "src" directory, then imports work correctly without modification.

python -m unittest discover -s ../test

If you want to put that in a batch file in your project directory, you can do this:

setlocal & cd src & python -m unittest discover -s ../test

@aliopi 2017-07-26 21:43:45

What's the usual way of actually running the tests

I use Python 3.6.2

cd new_project

pytest test/

To install pytest: sudo pip install pytest

I didn't set any path variable and my imports are not failing with the same "test" project structure.

I commented out this stuff: if __name__ == '__main__' like this:

import antigravity

class TestAntigravity(unittest.TestCase):

    def test_something(self):

        # ... test stuff here

# if __name__ == '__main__':
#     if __package__ is None:
#         import something
#         sys.path.append(path.dirname(path.dirname(path.abspath(__file__))))
#         from .. import antigravity
#     else:
#         from .. import antigravity
#     unittest.main()

@rolika 2017-07-25 14:01:44

Following is my project structure:

 - project:
 - tests:

I found it better to import in the setUp() method:

import unittest
import sys    

class ItemTest(unittest.TestCase):

    def setUp(self):
        sys.path.insert(0, "../project")
        from project import item
        # further setup using this import

    def test_item_props(self):
        # do my assertions

if __name__ == "__main__":

@Derek Soike 2017-06-28 23:45:13

Solution/Example for Python unittest module

Given the following project structure:

 ├── project_name
 |    ├── models
 |    |    └──
 |    └──
 └── test
      ├── models
      |    └──

You can run your project from the root directory with python project_name, which calls ProjectName/project_name/

To run your tests with python test, effectively running ProjectName/test/, you need to do the following:

1) Turn your test/models directory into a package by adding a file. This makes the test cases within the sub directory accessible from the parent test directory.

# ProjectName/test/models/

from .test_thing_1 import Thing1TestCase        

2) Modify your system path in test/ to include the project_name directory.

# ProjectName/test/

import sys
import unittest


loader = unittest.TestLoader()
testSuite ='test')
testRunner = unittest.TextTestRunner(verbosity=2)

Now you can successfully import things from project_name in your tests.

# ProjectName/test/models/    

import unittest
from project_name.models import Thing1  # this doesn't work without 'sys.path.append' per step 2 above

class Thing1TestCase(unittest.TestCase):

    def test_thing_1_init(self):
        thing_id = 'ABC'
        thing1 = Thing1(thing_id)

@Vlad Bezden 2017-05-27 23:45:43

If you use VS Code and your tests are located on the same level as your project then running and debug your code doesn't work out of the box. What you can do is change your launch.json file:

    "version": "0.2.0",
    "configurations": [
            "name": "Python",
            "type": "python",
            "request": "launch",
            "stopOnEntry": false,
            "pythonPath": "${config:python.pythonPath}",
            "program": "${file}",
            "cwd": "${workspaceRoot}",
            "env": {},
            "envFile": "${workspaceRoot}/.env",
            "debugOptions": [

The key line here is envFile

"envFile": "${workspaceRoot}/.env",

In the root of your project add .env file

Inside of your .env file add path to the root of your project. This will temporarily add


path to your project and you will be able to use debug unit tests from VS Code

@Pierre 2014-06-17 14:49:19

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:


You can just run:

$ cd new_project
$ python -m unittest test_antigravity

For a directory structure like yours:

├── antigravity
│   ├──         # make it a package
│   └──
└── test
    ├──         # also make test a package

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

$ 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.

@Mike3d0g 2015-05-18 02:49:41

python -m unittest discover will find and run tests in the test directory if they are named test*.py. If you named the subdirectory tests, use python -m unittest discover -s tests, and if you named the test files, use python -m unittest discover -s tests -p '*' File names can use underscores but not dashes.

@walkerrandophsmith 2015-10-01 19:03:27

I am new to python. Can you give an example of what would contain, given the directory structure that resembles the second example. I can imagine my question is really what is the difference between package and module, but would like insight into their distinction within the context of this scenario. Thank you!

@Alexander McFarlane 2016-06-15 04:08:10

Note that with this solution you cannot use explicit paths in e.g. import .antigravity will not work it must be import antigravity

@Chris 2016-12-12 15:27:46

What happens when you have a file like "test/" along side of "test/" that contains common test code? Do we simple reference that module with a relative import? or "from test.common import blah"?

@Pierre 2016-12-12 16:49:48

@Chris yes you can import from common using both ways, from .common import whatever or from test.common import whatever.

@expz 2016-12-22 21:45:25

This fails for me on Python 3 with the error ImportError: No module named 'test.test_antigravity' because of a conflict with the test sub-module of the unittest library. Maybe an expert can confirm and change the answer sub-directory name to e.g., 'tests' (plural).

@Drunken Master 2017-05-02 20:11:10

My still throws an import error for both import antigravity and from antigravity import antigravity, as well. I have both files and I am calling python3 -m unittest discover from the new project directory. What else could be wrong?

@matanster 2018-01-30 07:12:47

Anything that works for python 3?

@Radu Gabriel 2018-02-01 13:08:17

@DrunkenMaster in test/ add import os,sys; sys.path.insert(0, os.path.abspath('..'))

@johnson 2018-06-02 09:04:41

@matanster This works in python 3. I don't even have to use discover: python -m unittest seems to work like discover

@Francois 2018-08-02 13:32:15

file test/ is crucial here, even if empty

@ryan 2018-10-11 21:31:58

@Mike3d0g not sure if you meant to imply that the directory name test is special...but just for the record, it isn't. :P python -m unittest discover works with test files in tests/ just as well as test/.

@Mike3d0g 2018-10-13 13:15:41

@ryan It's been a while but I think I meant that tests named test*.py are special, like '' or '', because python -m unittest discover will find those test files automatically. But if you've named them '' or '', then you have to use python -m unittest discover -p '*'.

@stw_dev 2010-06-07 19:30:11

I generally create a "run tests" script in the project directory (the one that is common to both the source directory and test) that loads my "All Tests" suite. This is usually boilerplate code, so I can reuse it from project to project.

import unittest
import test.all_tests
testSuite = test.all_tests.create_test_suite()
text_runner = unittest.TextTestRunner().run(testSuite)

test/ (from How do I run all Python unit tests in a directory?)

import glob
import unittest

def create_test_suite():
    test_file_strings = glob.glob('test/test_*.py')
    module_strings = ['test.'+str[5:len(str)-3] for str in test_file_strings]
    suites = [unittest.defaultTestLoader.loadTestsFromName(name) \
              for name in module_strings]
    testSuite = unittest.TestSuite(suites)
    return testSuite

With this setup, you can indeed just include antigravity in your test modules. The downside is you would need more support code to execute a particular test... I just run them all every time.

@z33k 2018-03-26 19:14:09

I also wanted a run tests script in the project directory and found a lot cleaner way to do it. Highly recommended.

@Ned Batchelder 2009-12-13 16:24:53

Use develop to make your working directory be part of the installed Python environment, then run the tests.

@Major Major 2009-12-13 16:43:09

This gets me an invalid command 'develop' and this option isn't mentioned if I ask for --help-commands. Does there need to be something in the itself for this to work?

@Major Major 2009-12-13 16:54:39

It's OK - the problem was I was missing an import setuptools from my file. But I guess that does go to show that this won't work all the time for other people's modules.

@Eric Smith 2014-02-04 23:39:09

If you have pip, you can use that to install your package in "editable" mode: pip install -e . This likewise adds the package to the Python environment without copying the source, allowing you to continue to edit it where it lies.

@Carl Meyer 2014-07-15 00:57:09

pip install -e . is the exact same thing as python develop, it just monkeypatches your to use setuptools even if it doesn't actually, so it works either way.

Related Questions

Sponsored Content

25 Answered Questions

[SOLVED] Is there a way to run Python on Android?

51 Answered Questions

21 Answered Questions

[SOLVED] How do I list all files of a directory?

  • 2010-07-08 19:31:22
  • duhhunjonn
  • 3784052 View
  • 3474 Score
  • 21 Answer
  • Tags:   python directory

15 Answered Questions

[SOLVED] How do I run all Python unit tests in a directory?

25 Answered Questions

[SOLVED] How can I safely create a nested directory?

11 Answered Questions

[SOLVED] How to find if directory exists in Python

  • 2012-01-19 21:03:20
  • David542
  • 1038424 View
  • 1056 Score
  • 11 Answer
  • Tags:   python directory

5 Answered Questions

[SOLVED] Running python script inside ipython

  • 2012-07-31 15:55:55
  • Tyler Durden
  • 152515 View
  • 113 Score
  • 5 Answer
  • Tags:   python path ipython

1 Answered Questions

[SOLVED] Python package structure, for running unit tests

Sponsored Content