{
  "id": "python/unittest-subtest-attribute-error",
  "signature": "AttributeError: 'TestCase' object has no attribute 'subTest'",
  "signature_zh": "AttributeError: 'TestCase' 对象没有属性 'subTest'",
  "regex": "AttributeError:\\ 'TestCase'\\ object\\ has\\ no\\ attribute\\ 'subTest'",
  "domain": "python",
  "category": "type_error",
  "subcategory": null,
  "root_cause": "subTest was added in Python 3.4; running on an older interpreter, or the test class inherits from a custom base that shadows TestCase without calling super().__init__.",
  "root_cause_type": "generic",
  "root_cause_zh": "subTest 在 Python 3.4 中引入；运行在更旧的解释器上，或测试类继承自自定义基类但未调用 super().__init__ 导致 TestCase 未正确初始化。",
  "versions": [
    {
      "version": "3.9",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "3.10",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "3.11",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "",
      "why_fails": "Stops at the first failure; loses the ability to report all subtest failures.",
      "fail_rate": 0.5,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "Subtest failures are silently swallowed, so tests pass falsely.",
      "fail_rate": 0.9,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "",
      "success_rate": 0.93,
      "how": "# ensure base class calls super:\nclass Base(unittest.TestCase):\n    def __init__(self, *args, **kwargs):\n        super().__init__(*args, **kwargs)\n\n# then subTest works\nwith self.subTest(i=i):\n    self.assertEqual(f(i), i)",
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "success_rate": 0.95,
      "how": "import pytest\n\n@pytest.mark.parametrize('i', range(10))\ndef test_f(i):\n    assert f(i) == i",
      "condition": "",
      "sources": []
    }
  ],
  "workarounds_zh": [],
  "transition_graph": {
    "leads_to": [],
    "preceded_by": [],
    "frequently_confused_with": []
  },
  "official_doc_url": null,
  "official_doc_section": null,
  "error_code": null,
  "verification_tier": "ai_generated",
  "confidence": 0.84,
  "fix_success_rate": 0.8,
  "resolvable": "true",
  "first_seen": "2024-01-15",
  "last_confirmed": "2025-01-01",
  "last_updated": "2025-01-01",
  "evidence_count": 0,
  "tags": [],
  "locale": "en",
  "aliases": []
}