Skip to content

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.py
project/
  src/
    app.py
  tests/
    test_app.py

Running discovery

From the project root:

python -m unittest discover
python -m unittest discover

To specify a tests folder:

python -m unittest discover -s tests
python -m unittest discover -s tests

Run a specific test module

python -m unittest tests.test_app
python -m unittest tests.test_app

Tip

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 coffee

Was this page helpful?

Let us know how we did