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
text
project/
src/
app.py
tests/
test_app.pytext
project/
src/
app.py
tests/
test_app.pyRunning discovery
From the project root:
bash
python -m unittest discoverbash
python -m unittest discoverTo specify a tests folder:
bash
python -m unittest discover -s testsbash
python -m unittest discover -s testsRun a specific test module
bash
python -m unittest tests.test_appbash
python -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
