patternpythonCriticalCanonical
Running unittest with typical test directory structure
Viewed 0 times
withdirectoryunittestrunningtypicalstructuretest
Problem
The very common directory structure for even a simple Python module seems to be to separate the unit tests into their own
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
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."
test directory:new_project/
antigravity/
antigravity.py
test/
test_antigravity.py
setup.py
etc.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 test_antigravity.py 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."
Solution
The best solution in my opinion is to use the
For example for a directory structure like this:
You can just run:
For a directory structure like yours:
And in the test modules inside the
Running a single test module:
To run a single test module, in this case
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
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
This will run all the
Here you can find the updated official documentation of
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.pyYou can just run:
$ cd new_project
$ python -m unittest test_antigravityFor 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.pyAnd 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_objectRunning a single test module:
To run a single test module, in this case
test_antigravity.py:$ cd new_project
$ python -m unittest test.test_antigravityJust 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_methodRunning 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
$ # Also works without discover for Python 3
$ # as suggested by @Burrito in the comments
$ python -m unittestThis will run all the
test*.py modules inside the test package.Here you can find the updated official documentation of
discovery.Code Snippets
new_project
├── antigravity.py
└── test_antigravity.py$ cd new_project
$ python -m unittest test_antigravitynew_project
├── antigravity
│ ├── __init__.py # make it a package
│ └── antigravity.py
└── test
├── __init__.py # also make test a package
└── test_antigravity.py# 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$ cd new_project
$ python -m unittest test.test_antigravityContext
Stack Overflow Q#1896918, score: 945
Revisions (0)
No revisions yet.