python config_error ai_generated true

Ran 0 tests in 0.000s NO TESTS RAN

ID: python/unittest-test-discovery-zero-tests

Also available as: JSON · Markdown · 中文
80%Fix Rate
89%Confidence
0Evidence
2024-04-03First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
3.x active — — —

Root Cause

unittest discovery requires test files to match `test*.py` and classes to subclass `unittest.TestCase`. Common causes: file named `tests_foo.py`, class not inheriting TestCase, or discovery started from the wrong directory without `-s`.

generic

中文

unittest 发现要求测试文件匹配 `test*.py`,且类必须继承 `unittest.TestCase`。常见原因:文件名为 `tests_foo.py`、类未继承 TestCase、或从错误目录启动发现且未使用 `-s`。

Workarounds

  1. 96% success
    # tests/test_math.py
    import unittest
    
    class TestMath(unittest.TestCase):
        def test_add(self):
            self.assertEqual(1 + 1, 2)
    
    # run
    python -m unittest discover -s tests -p "test*.py" -v
  2. 93% success
    python -m unittest tests.test_math -v
    # or
    python -m unittest tests.test_math.TestMath.test_add

Dead Ends

Common approaches that don't work:

  1. 85% fail

    Discovery only collects TestCase subclasses; a plain class with test_ methods is ignored.

  2. 60% fail

    Default start dir is '.', but if tests live in a subdir not on the path, discovery finds nothing.