Running unittest with typical test directory structure
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
$ # Also works without discover for Python 3
$ # as suggested by @Burrito in the comments
$ python -m unittest
This will run all the test*.py
modules inside the test
package.
How do I run all Python unit tests in a directory?
With Python 2.7 and higher you don't have to write new code or use third-party tools to do this; recursive test execution via the command line is built-in. Put an __init__.py
in your test directory and:
python -m unittest discover <test_directory>
# or
python -m unittest discover -s <directory> -p '*_test.py'
You can read more in the python 2.7
or python 3.x unittest documentation.
Update for 2021:
Lots of modern python projects use more advanced tools like pytest. For example, pull down matplotlib or scikit-learn and you will see they both use it.
It is important to know about these newer tools because when you have more than 7000 tests you need:
- more advanced ways to summarize what passes, skipped, warnings, errors
- easy ways to see how they failed
- percent complete as it is running
- total run time
- ways to generate a test report
- etc etc
How to organise test files for unit testing in Python with sub folders
So after some reasearch (thanks @gftea) I found that in each __init__
file, doing from . import ___
is unecessary and can be removed (for my use case anyways).
Then I created these files in the root folder:
setup.py
from setuptools import setup
setup()
setup.cfg
[metadata]
name = <NAME>
version = 0.0.1
description = <DESCRIPTION>
long_description = file: README.md
long_description_content_type = text/markdown
url = <GITHUB REPOSITORY URL>
author = <AUTHOR(S)>
license = GPL-3.0
license_file = LICENSE
classifiers =
License :: OSI Approved :: GNU General Public License v3 (GPLv3)
Programming Language :: Python :: 3
Programming Language :: Python :: 3 :: Only
Programming Language :: Python :: 3.8
Programming Language :: Python :: 3.9
Programming Language :: Python :: 3.10
[options]
packages = find:
install_requires =
python-dotenv==0.19.2
requests==2.27.1
python_requires = >=3.8
tests_require =
pytest
coverage
[options.packages.find]
exclude =
tests*
[options.extras_require]
dev =
black==22.1.0
pylint==2.12.2
Afterwards I installed my package using pip install -e .
Then in the test python file I can do from project.util.some_file import SomeClass
Related Topics
How to Print a Variable Name in Python
What Are the Differences Between the Urllib, Urllib2, Urllib3 and Requests Module
How to Emulate a Do-While Loop
Calling Parent Class _Init_ with Multiple Inheritance, What's the Right Way
Print List Without Brackets in a Single Row
"Ssl Module in Python Is Not Available" When Installing Package with Pip3
Custom Sorting in Pandas Dataframe
Are Python Variables Pointers? or Else, What Are They
Security of Python's Eval() on Untrusted Strings
Extracting Just Month and Year Separately from Pandas Datetime Column
Import CSV File as a Pandas Dataframe
Can't Set Attributes on Instance of "Object" Class
Slice 2D Array into Smaller 2D Arrays
How to Parse Dates with -0400 Timezone String in Python
How to Get Multiline Input from the User
How to Hide the Console When I Use Os.System() or Subprocess.Call()