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.
Answer from Travis Bear on Stack OverflowWith 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.
In python 3, if you're using unittest.TestCase:
- You must have an empty (or otherwise)
__init__.pyfile in yourtestdirectory (must be namedtest/) - Your test files inside
test/match the patterntest_*.py.- They can be inside a subdirectory under
test/. Those subdirs can be named as anything, but they all need to have an__init__.pyfile in them
- They can be inside a subdirectory under
- Your test function names should start with
test_. Ex:test_text_matcher
Then, you can run all the tests with:
python -m unittest
Pytest or Unittest in 2020
Unittest or Pytest?
Best unit-test practices?
pytest has its own runner/command line so once you have a directory full of tests, you can just run pytest in your terminal and it'll run all your unit tests. That's fairly standard and it picks up unittest style tests as well.