Test Discovery and Running Tests
Test discovery rules (common pattern)
Unittest can discover tests when:
- files match
test*.pytest*.py - tests are under a folder like
tests/tests/ - test methods start with
test_test_
Typical project layout
project/
src/
app.py
tests/
test_app.pyproject/
src/
app.py
tests/
test_app.pyRunning discovery
From the project root:
python -m unittest discoverpython -m unittest discoverTo specify a tests folder:
python -m unittest discover -s testspython -m unittest discover -s testsRun a specific test module
python -m unittest tests.test_apppython -m unittest tests.test_appTip
If discovery finds 0 tests, check:
- filenames
- class inherits from
unittest.TestCaseunittest.TestCase - methods start with
test_test_
๐งช Try It Yourself
Exercise 1 โ Write a unittest TestCase
Exercise 2 โ assertRaises
Exercise 3 โ setUp and tearDown
If this helped you, consider buying me a coffee โ
Buy me a coffeeWas this page helpful?
Let us know how we did
