{"meta":{"title":"诊断测试中的失败","intro":"副驾驶聊天 可以帮助了解测试失败的原因，并建议解决方法。","product":"GitHub Copilot","breadcrumbs":[{"href":"/zh/copilot","title":"GitHub Copilot"},{"href":"/zh/copilot/tutorials","title":"教程"},{"href":"/zh/copilot/tutorials/copilot-chat-cookbook","title":"GitHub Copilot Chat 指南"},{"href":"/zh/copilot/tutorials/copilot-chat-cookbook/debug-errors","title":"调试中出现的错误"},{"href":"/zh/copilot/tutorials/copilot-chat-cookbook/debug-errors/diagnose-test-failures","title":"诊断测试失败"}],"documentType":"article"},"body":"# 诊断测试中的失败\n\n副驾驶聊天 可以帮助了解测试失败的原因，并建议解决方法。\n\n副驾驶聊天 可以分析测试失败并帮助识别潜在原因。\n\n## 示例场景：测试在本地通过，但在 CI 中失败\n\n考虑一个场景，在这个场景中，你的测试在本地计算机上通过，但在 CI 中有时会失败。 副驾驶聊天 可以帮助确定失败的原因。\n\n在此示例中，所测试的代码定义了一个简单的订单服务（`order.py`），并且有一个相应的测试来检查订单是否是今天创建的（`test_order_service.py`）。\n\n## 示例提示\n\n以下提示提供含相关代码和测试文件（使用 `#file:`）的 Copilot，并且包含 CI 故障产生的相关摘录的复制/粘贴。\n\n```copilot copy\nPlease take a look at this CI failure message. The test passes locally, but intermittently fails in CI. Can you help me figure out if this looks like a code bug, environment issue, or a flaky test?\n\nFailure:\n\n___ TestOrderService.test_order_created_today ___\n>       assert order[\"created_date\"] == date.today()\nE       AssertionError: assert datetime.date(2024, 1, 15) == datetime.date(2024, 1, 16)\n\ntest_order_service.py:45: AssertionError\n\n#file:order.py\n#file:test_order_service.py\n\n```\n\n## 示例响应\n\n> \\[!NOTE] 以下响应是示例。 副驾驶聊天 的回答是不确定的，因此你可能会得到与这里所显示的不同的回答。\n\n副驾驶聊天 注意到日期恰好相差一天，并识别到这可能是**时区**或**时间边界**问题。\n\n本地计算机和 CI 运行程序可能使用不同的时区设置或派生 `today` 自不同的时钟（UTC 与本地时间），因此，当测试在午夜附近运行时， `date.today()` 可以在每个环境中返回不同的日期。\n\n副驾驶聊天 建议将故障视为环境/时间假设导致的测试不稳定性（而不是逻辑 Bug），并通过跨环境实现 `today` 计算方式标准化来解决这种情况。\n\n## 示例方案 2：间歇性测试失败\n\n假设测试有时通过，有时在同一台计算机上失败。 副驾驶聊天 可以比较成功和失败运行产生的日志，帮助识别原因。\n\n在此示例中，受测代码使用 `order_service.py` 中的后台作业异步更新订单状态，并在 `test_order_service.py` 中测试断言最终状态为 `\"processed\"`。\n\n## 示例提示\n\n以下提示提供含失败消息、成功和失败运行的日志摘录以及相关代码文件（使用 `#file:`）的 Copilot。\n\n```copilot copy\nThis test passes sometimes and fails sometimes. Can you compare the logs and help me figure out why?\n\nFailure message:\n\n>       assert order.status == \"processed\"\nE       AssertionError: assert \"pending\" == \"processed\"\n\ntest_order_service.py:62: AssertionError\n\nLogs from a passing run:\n[DEBUG] Created order #1234\n[DEBUG] Background job started for order #1234\n[DEBUG] Background job completed (52ms)\n[DEBUG] Checking order status\n[DEBUG] Order #1234 status: processed\n\nLogs from the failing run:\n[DEBUG] Created order #1234\n[DEBUG] Background job started for order #1234\n[DEBUG] Checking order status\n[DEBUG] Order #1234 status: pending\n\n#file:order_service.py\n#file:test_order_service.py\n```\n\n## 示例响应\n\n> \\[!NOTE] 以下响应是示例。 副驾驶聊天 的回答是不确定的，因此你可能会得到与这里所显示的不同的回答。\n\n副驾驶聊天 比较两个日志，注意到在成功运行中，后台作业在状态检查*之前*完成，而在失败运行中，在作业仍在运行时检查状态。 副驾驶聊天 注意到，由于测试不会等待后台作业完成，因此这是一种**争用条件**。\n\n副驾驶聊天 建议在断言之前添加一种机制，例如同步运行作业、等待完成（例如，通过回调）或轮询，以便确保作业完成。\n\n## 延伸阅读\n\n* [GitHub Copilot 对话助手的提示设计](/zh/copilot/using-github-copilot/prompt-engineering-for-github-copilot)\n* [使用 GitHub Copilot 的最佳做法](/zh/copilot/using-github-copilot/best-practices-for-using-github-copilot)"}