> For the complete documentation index, see [llms.txt](https://ai-native-development.gitbook.io/docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://ai-native-development.gitbook.io/docs/ja/testing/creating-unit-tests.md).

# ユニットテストの作成

GitHub Copilot を使用してユニットテストを作成することができます。

[![](https://img.shields.io/badge/Lv3-Mature_Best_Practice-brightgreen)](https://github.com/orgs/AI-Native-Development/projects/1/)

## Description

テストはソフトウェア開発プロセスの基本的な部分であり、コードが設計どおりであり、意図した通りに動作することを確認します。システムの個々のコンポーネントをテストするユニットテストの作成は、チャレンジングで時間のかかる作業です。GitHub Copilotを使用すると、このプロセスがより効率的になります。開発者のAliceさんがGitHub Copilotをどのように活用してアプリケーションのユニットテストを記述し、作業量を減少させ、効率を向上させるかを探ってみましょう。 このパターンは、機能テストや API テストにも適用可能です。

### Example

徹底的にテストする必要のある JavaScript の関数に取り組んでいます。GitHub Copilotの助けを借りて、必要なユニットテストを素早く生成できます。

以下は、テストしたいシンプルな関数です:

```javascript
function add(x, y) {
  return x + y;
}
```

そして、GitHub Copilotの支援を受けてユニットテストを作成する方法は次のとおりです:

```javascript
const assert = require('assert');

describe('add関数', () => {
  it('2つの数値を正しく加算する必要があります', () => {
    assert.equal(add(2, 3), 5);
  });
});
```

## Exercise

* **エクササイズ1**: GitHub Copilotを使用して2つの数値を掛ける関数のユニットテストを作成してください。
* **エクササイズ2**: Copilotを活用して、nullやundefinedの値を処理するなど、さまざまなエッジケースのテストスイートを作成してください。
* **エクササイズ3**: 現在のプロジェクトを振り返り、テストが不足しているコードの部分を特定し、Copilotを使用してユニットテストを作成してください。

## Checklist for Further Learning

* 自分のテストが包括的であり、すべての可能なシナリオをカバーしていることをどのように確認できますか?
* GitHub Copilot がシナリオを全くカバーしなかった際にどのようなプロンプトを追加しますか?
* 他の種類のテスト(E2Eテスト、統合テスト、機能テストなど) に対して、GitHub Copilot はどのように有益であり、それらを記述する際に GitHub Copilotはどのように支援できるか?


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://ai-native-development.gitbook.io/docs/ja/testing/creating-unit-tests.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
