{"meta":{"title":"生成单元测试","intro":"为代码创建有针对性的单元测试。","product":"GitHub Copilot","breadcrumbs":[{"href":"/zh/copilot","title":"GitHub Copilot"},{"href":"/zh/copilot/tutorials","title":"教程"},{"href":"/zh/copilot/tutorials/customization-library","title":"定制化库"},{"href":"/zh/copilot/tutorials/customization-library/prompt-files","title":"提示文件"},{"href":"/zh/copilot/tutorials/customization-library/prompt-files/generate-unit-tests","title":"生成单元测试"}],"documentType":"article"},"body":"# 生成单元测试\n\n为代码创建有针对性的单元测试。\n\n> \\[!NOTE]\n> \\*\n> Copilot 提示文件位于 公开预览 并且可能会更改。\n> 提示文件仅适用于 VS Code、Visual Studio 和 JetBrains IDE。 请参阅 [关于自定义GitHub Copilot 响应](/zh/copilot/concepts/prompting/response-customization#about-prompt-files)。\n>\n> * 有关社区贡献的特定语言和场景的提示文件示例，请参阅 GitHub Copilot 网站。\n\n此提示文件针对特定功能或方法生成有针对性的单元测试，强调实际测试用例和可维护的代码。\n\n## 单元测试生成提示\n\n```text copy\n---\nagent: 'agent'\ndescription: 'Generate unit tests for selected functions or methods'\n---\n\n## Task\n\nAnalyze the selected function/method and generate focused unit tests that thoroughly validate its behavior.\n\n## Test Generation Strategy\n\n1. **Core Functionality Tests**\n   - Test the main purpose/expected behavior\n   - Verify return values with typical inputs\n   - Test with realistic data scenarios\n\n2. **Input Validation Tests**\n   - Test with invalid input types\n   - Test with null/undefined values\n   - Test with empty strings/arrays/objects\n   - Test boundary values (min/max, zero, negative numbers)\n\n3. **Error Handling Tests**\n   - Test expected exceptions are thrown\n   - Verify error messages are meaningful\n   - Test graceful handling of edge cases\n\n4. **Side Effects Tests** (if applicable)\n   - Verify external calls are made correctly\n   - Test state changes\n   - Validate interactions with dependencies\n\n## Test Structure Requirements\n\n- Use existing project testing framework and patterns\n- Follow AAA pattern: Arrange, Act, Assert\n- Write descriptive test names that explain the scenario\n- Group related tests in describe/context blocks\n- Mock external dependencies cleanly\n\nTarget function: ${input:function_name:Which function or method should be tested?}\nTesting framework: ${input:framework:Which framework? (jest/vitest/mocha/pytest/rspec/etc)}\n\n## Guidelines\n\n- Generate 5-8 focused test cases covering the most important scenarios\n- Include realistic test data, not just simple examples\n- Add comments for complex test setup or assertions\n- Ensure tests are independent and can run in any order\n- Focus on testing behavior, not implementation details\n\nCreate tests that give confidence the function works correctly and help catch regressions.\n```\n\n## 如何使用此提示文件\n\n1. 将上述内容另存为 `generate-unit-tests.prompt.md`，并将其保存在 `.github/prompts` 文件夹中。\n2. 打开包含要测试的函数的代码文件。 （可选）可以突出显示特定函数。\n3. 在 Visual Studio Code 中，显示 Copilot Chat 视图，然后输入 `/generate-unit-tests`。 例如，（可选）也可以通过键入 `function_name=fetchActivities` 和 `framework=pytest` 来指定目标函数和测试框架。\n\n## 其他阅读材料\n\n* Visual Studio Code 文档中的Visual Studio Code - 有关如何创建和使用提示文件的信息\n* [关于自定义GitHub Copilot 响应](/zh/copilot/concepts/prompting/response-customization) - GitHub Copilot 中的响应自定义概述\n* [出色的 GitHub Copilot 自定义内容](https://github.com/github/awesome-copilot/blob/main/docs/README.prompts.md) - 社区提供的自定义提示文件的仓库以及特定语言和场景的其他自定义内容"}