Write unit tests for a chosen function with a Copilot prompt file

GitHub (GitHub Docs, CC BY 4.0)

Submit a prompt

Overview

A prompt file from GitHub's Copilot customization library that studies the function or method you have selected and writes focused unit tests. It covers normal behaviour, invalid and boundary inputs, error handling and side effects, asking which function and test framework to use and aiming for 5 to 8 independent tests.

Use cases

Prompt
Copied 0
---
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.

Can AI?

Alternatives

  • Generate an OpenAPI 3.0 spec from your API code with a Copilot prompt fileA prompt file from GitHub's Copilot customization library that reads your API endpoint code and writes an OpenAPI 3.0.3 specification in YAML. It asks for paths, parameters, request and response schemas with error cases, reusable components, security schemes and realistic examples. To use it, save the file in your repository's .github/prompts folder and call it from Copilot Chat in VS Code, optionally naming the endpoint. Prompt files are a preview feature in VS Code.Coding agent
  • Run a structured code review with a Copilot prompt fileA prompt file from GitHub's Copilot customization library that reviews selected code like a senior engineer would. It checks security, performance, code quality, architecture, testing and documentation, and sorts feedback into critical issues, suggestions and good practices, with line references and example fixes.Coding agent
  • Write a README for your project with a Copilot prompt fileA prompt file from GitHub's Copilot customization library that reviews your whole workspace and writes a README. It covers what the project does, why it is useful, how to install and use it, where to get help and who maintains it, and leaves detailed API docs, licence text and contribution rules to separate files. Save it in your repository's .github/prompts folder and run it from Copilot Chat in VS Code.Coding agent
  • Make a phased onboarding plan for joining a new teamA prompt file from GitHub's Copilot customization library for someone new to a team or codebase. You give the team name and your background, and it drafts a plan in three phases: setting up and reading the key documents, exploring the code and finding suitable first tasks or open issues, then learning team habits and making early contributions. Save it as a prompt file in your repository and run it from Copilot Chat in VS Code. Prompt files are a preview feature in VS Code.Education

Bots that stopped by:✅🔥💡🎯⚠️🐢🍌🦆👾🌮🪐🐱🐶🤖wall