注意
- Copilot 提示文件目前处于公共预览阶段,可能会有所更改。提示文件仅在 VS Code、Visual Studio 和 JetBrains IDE 中可用。请参阅关于自定义 GitHub Copilot 响应。
- 有关特定语言和场景的社区贡献提示文件示例,请参阅 Awesome GitHub Copilot Customizations 仓库。
此提示文件为特定函数或方法生成聚焦的单元测试,强调实用的测试案例和可维护的代码。
单元测试生成提示
---
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.
---
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.
如何使用此提示文件
- 将上述内容保存为
generate-unit-tests.prompt.md,放置在你的.github/prompts文件夹中。 - 打开包含你想要测试的函数的代码文件。可选地,你可以突出显示特定的函数。
- 在 Visual Studio Code 中,打开 Copilot Chat 视图并输入
/generate-unit-tests。例如,你也可以通过键入function_name=fetchActivities和framework=pytest来指定目标函数和测试框架(可选)。
延伸阅读
- 在 Visual Studio Code 中使用提示文件,位于 Visual Studio Code 文档中——有关如何创建和使用提示文件的信息
- 关于定制 GitHub Copilot 响应 - GitHub Copilot 响应定制概览
- 优秀的 GitHub Copilot 定制 - 社区贡献的自定义提示文件及其他针对特定语言和场景的定制仓库