python config_error ai_generated true

Ran 0 tests in 0.000s NO TESTS RAN

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

其他格式: JSON · Markdown 中文 · English
80%修复率
89%置信度
0证据数
2024-04-03首次发现

版本兼容性

版本状态引入弃用备注
3.x active — — —

根因分析

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

English

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

解决方案

  1. 96% 成功率
    # 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% 成功率
    python -m unittest tests.test_math -v
    # or
    python -m unittest tests.test_math.TestMath.test_add

无效尝试

常见但无效的做法:

  1. 85% 失败

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

  2. 60% 失败

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