用提示文件驱动 Copilot 生成聚焦单元测试
该资料介绍一个 GitHub Copilot 提示文件,用于为选定函数或方法生成单元测试。提示文件按核心功能、输入校验、错误处理与副作用四类策略组织用例,并要求遵循 AAA 结构与项目既有测试框架。
说明 - copilot 即时文件处于公开预览中,并且可以更改。 即时文件仅可在 VS 代码、 Visual Studio 和 JetBrains IDES 中获取。 请参见 [提示文件用于 GitHub 副驾驶](<https://docs.github.com/en/copilot/concepts/prompting-and-context/prompt-files>). - 关于由社区提供的关于特定语言和情景的即时文件的例子,请参见: [很棒的 GitHub 副驾驶自定义](<https://awesome-copilot.github.com/>) (原始内容存档于2017-09-31). site. 这种即时文件生成针对特定功能或方法的焦点单位测试,强调实用测试案例和可维护代码. ## [单位测试生成提示](<https://docs.github.com/en/copilot/tutorials/customization-library/prompt-files/generate-unit-tests#unit-test-generation-prompt>) ```text --- agent: 'agent' description: 'Generate unit tests for selected functions or methods' --- ## Task Analyze the selected function/method and generate focused unit tests that thoroughly validate its behavior. ## Test Generation Strategy 1. **Core Functionality Tests** - Test the main purpose/expected behavior - Verify return values with typical inputs - Test with realistic data scenarios 2. **Input Validation Tests** - Test with invalid input types - Test with null/undefined values - Test with empty strings/arrays/objects - Test boundary values (min/max, zero, negative numbers) 3. **Error Handling Tests** - Test expected exceptions are thrown - Verify error messages are meaningful - Test graceful handling of edge cases 4. **Side Effects Tests** (if applicable) - Verify external calls are made correctly - Test state changes - Validate interactions with dependencies ## Test Structure Requirements - Use existing project testing framework and patterns - Follow AAA pattern: Arrange, Act, Assert - Write descriptive test names that explain the scenario - Group related tests in describe/context blocks - Mock external dependencies cleanly Target function: ${input:function_name:Which function or method should be tested?} Testing framework: ${input:framework:Which framework? (jest/vitest/mocha/pytest/rspec/etc)} ## Guidelines - Generate 5-8 focused test cases covering the most important scenarios - Include realistic test data, not just simple examples - Add comments for complex test setup or assertions - Ensure tests are independent and can run in any order - Focus on testing behavior, not implementation details Create tests that give confidence the function works correctly and help catch regressions. ``` ## [如何使用此快捷文件](<https://docs.github.com/en/copilot/tutorials/customization-library/prompt-files/generate-unit-tests#how-to-use-this-prompt-file>) 1. 将以上内容另存为 `generate-unit-tests.prompt.md` 在您的帐号中 `.github/prompts` 文件夹。 2. 打开包含您想要测试的函数的代码文件。可以选择,您可以突出显示一个特定的函数。 3. 在Visual Studio代码中,显示“副驾驶聊天”视图并输入 `/generate-unit-tests`。也可以通过输入指定目标函数和测试框架。 `function_name=fetchActivities` 财务报告和财务报告 `framework=pytest`比如说。 ## [进一步阅读](<https://docs.github.com/en/copilot/tutorials/customization-library/prompt-files/generate-unit-tests#further-reading>) - [在 Visual Studio 代码中使用快捷文件](<https://code.visualstudio.com/docs/copilot/customization/prompt-files>) 在Visual Studio代码文档中 - 关于如何创建和使用快速文件的信息 - [提示文件用于 GitHub 副驾驶](<https://docs.github.com/en/copilot/concepts/prompting-and-context/prompt-files>) - GitHub 的快速文件概览 - [很棒的 GitHub 副驾驶自定义](<https://github.com/github/awesome-copilot/blob/main/docs/README.prompts.md>) - 由社区贡献的定制快速文件和其他特定语言和情景定制的存储库
查看原始来源