{
  "id": "python/pytest-capsys-flush-buffering",
  "signature": "AssertionError: assert '' == 'hello\\n'\n  - \n  + hello",
  "signature_zh": "AssertionError: assert '' == 'hello\\n'\n  - \n  + hello",
  "regex": "AssertionError:\\ assert\\ ''\\ ==\\ 'hello\\\\n'\\\n\\ \\ \\-\\ \\\n\\ \\ \\+\\ hello",
  "domain": "python",
  "category": "runtime_error",
  "subcategory": null,
  "root_cause": "`capsys.readouterr()` returns empty because the code under test uses `print(..., flush=False)` and the buffer has not been flushed, or it writes to `sys.__stdout__` instead of `sys.stdout`.",
  "root_cause_type": "generic",
  "root_cause_zh": "`capsys.readouterr()` 返回空，因为被测代码使用了 `print(..., flush=False)` 且缓冲区尚未刷新，或者它写入的是 `sys.__stdout__` 而不是 `sys.stdout`。",
  "versions": [
    {
      "version": "7.x",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "8.x",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "",
      "why_fails": "Flushing is not time-based; the buffer may still be unflushed and the test is now flaky.",
      "fail_rate": 0.8,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "capfd captures file descriptors; if the code uses sys.__stdout__ directly it still bypasses both.",
      "fail_rate": 0.6,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "",
      "success_rate": 0.95,
      "how": "Force flush in the code under test or use print(..., flush=True):\n\nprint('hello', flush=True)",
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "success_rate": 0.9,
      "how": "Use capfd and ensure the code writes to sys.stdout:\n\ndef test_out(capfd):\n    my_func()\n    out, err = capfd.readouterr()\n    assert out == 'hello\\n'",
      "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.85,
  "fix_success_rate": 0.8,
  "resolvable": "true",
  "first_seen": "2024-10-05",
  "last_confirmed": "2025-01-01",
  "last_updated": "2025-01-01",
  "evidence_count": 0,
  "tags": [],
  "locale": "en",
  "aliases": []
}