« Back to Index

Python: Run Python tests using -m

View original Gist on GitHub

Run Python tests using -m.md

Syntax

python -m <path/to/test/directory/>.<name_of_test_file>

Example

Given the following directory structure:

└── test
    ├── __init__.py
    ├── foo.py
    └── bar.py

You can execute the tests in each file like so:

python -m test.foo
python -m test.bar

This requires the code to use:

if __name__ == '__main__':
    unittest.main()

See also: https://realpython.com/python-testing/#executing-your-first-test