# 単体テストを生成する

コードのための焦点を当てた単体テストを作成します。

> \[!NOTE]
>
> * Copilot プロンプト ファイルは パブリック プレビュー 段階であり、変更される可能性があります。 プロンプト ファイルは、VS Code、Visual Studio、および JetBrains IDE でのみ使用できます。「[GitHub Copilotの応答をカスタマイズする方法](/ja/copilot/concepts/prompting/response-customization#about-prompt-files)」を参照してください。
> * 特定の言語とシナリオ向けの、コミュニティに投稿されたプロンプト ファイルの例については、[Awesome GitHub Copilot Customizations](https://github.com/github/awesome-copilot/blob/main/docs/README.prompts.md) リポジトリを参照してください。

このプロンプト ファイルは、実用的なテスト ケースと保守が容易なコードに重点を置いて、特定の関数またはメソッドに焦点を当てた単体テストを生成します。

## 単体テスト生成プロンプト

```text copy
---
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.
```

## このプロンプト ファイルの使用方法

1. 上記の内容を `generate-unit-tests.prompt.md` として `.github/prompts` フォルダーに保存します。
2. テスト対象の関数を含むコード ファイルを開きます。 必要に応じて、特定の関数を強調表示できます。
3. Visual Studio Code で コパイロットチャット ビューに、「`/generate-unit-tests`」と入力します。 必要に応じて、`function_name=fetchActivities` と `framework=pytest` などを入力して、対象となる関数とテスト フレームワークを指定することもできます。

## 参考資料

* Visual Studio Code ドキュメントの「[Visual Studio Code でプロンプト ファイルを使う](https://code.visualstudio.com/docs/copilot/customization/prompt-files)」- プロンプト ファイルの作成と使用の方法に関する情報
* [GitHub Copilotの応答をカスタマイズする方法](/ja/copilot/concepts/response-customization) - GitHub Copilot での応答カスタマイズの概要
* [優れた GitHub Copilot カスタマイズ](https://github.com/github/awesome-copilot/blob/main/docs/README.prompts.md) - コミュニティに投稿されたカスタム プロンプト ファイルや、特定の言語やシナリオ向けのその他のカスタマイズのリポジトリ