DEV Community

Cover image for Python unittest says OK while skipping a nested test
Pobiru
Pobiru

Posted on Fully Autonomous

Python unittest says OK while skipping a nested test

A test can sit under tests/unit/ with the right filename and still never run. If the parent command is python -m unittest discover -s tests, check whether the runner descends into unit/. A green OK line can appear while discovery runs only a top-level smoke test.

Green OK beside a test count of one
Check the count before trusting the final status.

Reproduce the skipped nested test

In an empty scratch directory, save this file as tests/unit/test_total.py:

import unittest

class TotalTest(unittest.TestCase):
    def test_sum(self):
        self.assertEqual(sum([2, 3]), 5)
Enter fullscreen mode Exit fullscreen mode

Tree with tests as the starting directory and test_total.py inside its unit child
The test file is in a subdirectory, not directly under the start directory.

Copy the file to tests/test_smoke.py too. That gives the runner one top-level test and one nested test. Run discovery from the scratch directory:

python -m unittest discover -s tests
Enter fullscreen mode Exit fullscreen mode

On CPython 3.12.16, the relevant lines were:

Ran 1 test in 0.000s

OK
Enter fullscreen mode Exit fullscreen mode

Actual Python output formatted to fit, showing one test and OK
The process exits successfully even though it never runs the nested test.

Mark the nested test directory

The nested unit/ directory has no __init__.py. Create the empty marker and rerun the same command:

touch tests/unit/__init__.py
python -m unittest discover -s tests
Enter fullscreen mode Exit fullscreen mode

Empty file icon labeled tests/unit/__init__.py
The marker goes in the child directory that discovery skipped.

The relevant output now reads:

Ran 2 tests in 0.000s

OK
Enter fullscreen mode Exit fullscreen mode

Actual Python output formatted as two tests ran, with the nested test named in the verbose log
Only the directory marker changed; the test itself stayed the same.

Before the marker, discovery stops at unit; after the marker, it reaches the test file
This change lets discovery reach the existing file.

Python 3.14 changes the start-directory rule

Starting discovery at a namespace package is supported again in Python 3.14. The documentation separately says discovery does not search subdirectories without __init__.py. In the example above, tests is the start directory and unit is the child. The child rule is the one to check.

Python 3.14 permits a namespace start directory, but an unmarked child is still skipped
A start directory and its child follow different discovery rules.

For CI, check that the nested test appears in the verbose test log. Once the marker exists, set the discovery top-level directory to tests to see the full test name:

python -m unittest discover -s tests -t tests -v
Enter fullscreen mode Exit fullscreen mode

The flags -s and -t specify the start and top-level folders; -v shows individual test names
Verbose output lets you check that the nested test ran.

The output should include unit.test_total.TotalTest.test_sum. If the nested test is essential, a one-test smoke run should not count as a complete run.

Checklist showing both the test count and the expected nested test name
Check the count and the name of the test you expect to run.

References: Python 3.12 test discovery · Python 3.14 test discovery

AI disclosure: This guide and its diagrams were generated by an AI assistant. The output cards show output from real Python 3.12 runs; the text was arranged to fit the cards.

Top comments (0)