{"meta":{"title":"Generate unit tests","intro":"Create focused unit tests for your code.","product":"GitHub Copilot","breadcrumbs":[{"href":"/en/copilot","title":"GitHub Copilot"},{"href":"/en/copilot/tutorials","title":"Tutorials"},{"href":"/en/copilot/tutorials/customization-library","title":"Customization library"},{"href":"/en/copilot/tutorials/customization-library/prompt-files","title":"Prompt files"},{"href":"/en/copilot/tutorials/customization-library/prompt-files/generate-unit-tests","title":"Generate unit tests"}],"documentType":"article"},"body":"# Generate unit tests\n\nCreate focused unit tests for your code.\n\n> \\[!NOTE]\n>\n> * Copilot prompt files are in public preview and subject to change. Prompt files are only available in VS Code, Visual Studio, and JetBrains IDEs. See [About customizing GitHub Copilot responses](/en/copilot/concepts/prompting/response-customization#about-prompt-files).\n> * For community-contributed examples of prompt files for specific languages and scenarios, see the [Awesome GitHub Copilot Customizations](https://github.com/github/awesome-copilot/blob/main/docs/README.prompts.md) repository.\n\nThis prompt file generates focused unit tests for specific functions or methods, emphasizing practical test cases and maintainable code.\n\n## Unit test generation prompt\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## How to use this prompt file\n\n1. Save the above content as `generate-unit-tests.prompt.md` in your `.github/prompts` folder.\n2. Open the code file containing the function(s) you want tests for. Optionally, you can highlight a specific function.\n3. In Visual Studio Code, display the Copilot Chat view and enter `/generate-unit-tests`. Optionally, you can also specify the target function and testing framework by typing `function_name=fetchActivities` and `framework=pytest`, for example.\n\n## Further reading\n\n* [Use prompt files in Visual Studio Code](https://code.visualstudio.com/docs/copilot/customization/prompt-files) in the Visual Studio Code documentation - Information on how to create and use prompt files\n* [About customizing GitHub Copilot responses](/en/copilot/concepts/response-customization) - Overview of response customization in GitHub Copilot\n* [Awesome GitHub Copilot Customizations](https://github.com/github/awesome-copilot/blob/main/docs/README.prompts.md) - Repository of community-contributed custom prompt files and other customizations for specific languages and scenarios"}