{"meta":{"title":"GitHub Copilot を使用したテストの記述","intro":"Copilotを使用して単体テストと統合テストを生成し、コード品質の向上に役立ちます。","product":"GitHub Copilot","breadcrumbs":[{"href":"/ja/enterprise-cloud@latest/copilot","title":"GitHub Copilot"},{"href":"/ja/enterprise-cloud@latest/copilot/tutorials","title":"チュートリアル"},{"href":"/ja/enterprise-cloud@latest/copilot/tutorials/write-tests","title":"テストを記述する"}],"documentType":"article"},"body":"# GitHub Copilot を使用したテストの記述\n\nCopilotを使用して単体テストと統合テストを生成し、コード品質の向上に役立ちます。\n\n## はじめに\n\nGitHub Copilot は、テストを迅速に開発し、生産性を向上させるのに役立つ可能性があります。 この記事では、 Copilot を使用して単体テストと統合テストの両方を記述する方法について説明します。\nCopilotは基本的な関数のテストを生成するときに適切に機能しますが、複雑なシナリオでは、より詳細なプロンプトと戦略が必要です。 この記事では、 Copilot を使用してタスクを分解し、コードの正確性を確認する実際の例について説明します。\n\n## 前提条件\n\n開始する前に、次のものが必要です。\n\n* [\n  GitHub Copilot サブスクリプション プラン](/ja/enterprise-cloud@latest/copilot/get-started/plans)。\n* Visual Studio、 Visual Studio Code、または JetBrains IDE。\n* IDE にインストールされている [GitHub Copilot 拡張機能](/ja/enterprise-cloud@latest/copilot/how-tos/set-up/install-copilot-extension) 。\n\n## Copilot Chat を使用した単体テストの作成\n\nこのセクションでは、GitHub Copilot Chatを使用して、Python クラスの単体テストを生成する方法について説明します。 この例では、 Copilot を使用して、 `BankAccount`などのクラスの単体テストを作成する方法を示します。 テストの生成、テストの実行、結果の検証を Copilot に求める方法について説明します。\n\n### クラスの例: `BankAccount`\n\nまず、`BankAccount` クラスから始めましょう。これには、口座への預金、口座残高の引き出し、取得を行うメソッドが含まれています。\n`bank_account.py` リポジトリに新しいファイル GitHubを作成し、Pythonに次の`BankAccount` クラスを追加します。\n\n```python\nclass BankAccount:\n    def __init__(self, initial_balance=0):\n        if initial_balance < 0:\n            raise ValueError(\"Initial balance cannot be negative.\")\n        self.balance = initial_balance\n\n    def deposit(self, amount):\n        if amount <= 0:\n            raise ValueError(\"Deposit amount must be positive.\")\n        self.balance += amount\n\n    def withdraw(self, amount):\n        if amount <= 0:\n            raise ValueError(\"Withdrawal amount must be positive.\")\n        if amount > self.balance:\n            raise ValueError(\"Cannot withdraw more than the current balance.\")\n        self.balance -= amount\n\n    def get_balance(self):\n        return self.balance\n```\n\n### Copilot Chat を使用した単体テストの生成\n\nCopilot に対して、さまざまなシナリオに対応したテストを記述するように求めるプロンプトを作成できます。 最善の結果を得るために、テスト要件は具体的に記述してください。\n`BankAccount` クラスの単体テストを生成するために使用できるプロンプトの例を次に示します。\n\n**プロンプト:** BankAccount() 関数に対する包括的な単体テスト スイートを Python で開発します。\\_\\_ エッジ ケース、例外処理、データ検証など、さまざまなシナリオに対応する複数のテスト メソッドを記述します。\n\nこのプロンプトでは、いくつかの重要なことを行っています。\n\n* \\*\\*スコープ：\\*\\*Copilotは基本的なテスト以上のものを生成し、さまざまな潜在的なユース ケースをカバーするように、包括的な単体テストスイートを明示的に要求します。\n* **シナリオ**: このプロンプトは、テストに以下を含める必要があることを指定しています。\n  * **エッジ ケース**: 境界条件のテスト (負の金額の預金または引き出し、利用可能な残高を超える引き出しなど)。\n  * **例外処理**: 特定の無効なアクションが行われたときに発生させる必要があるエラーのテスト (利用可能な残高を超える引き出し、負の金額の預金など)。\n  * **データ検証**: 入力がメソッドによって正しく検証され、処理されることを確認します。\n\nCopilot では、次のような応答が返されます。\n\n```python\nimport unittest\nfrom bank_account import BankAccount\n\nclass TestBankAccount(unittest.TestCase):\n    def setUp(self):\n        self.account = BankAccount()\n\n    def test_initial_balance(self):\n        self.assertEqual(self.account.get_balance(), 0)\n\n    def test_deposit_positive_amount(self):\n        self.account.deposit(100)\n        self.assertEqual(self.account.get_balance(), 100)\n\n    def test_withdraw_within_balance(self):\n        self.account.deposit(100)\n        self.account.withdraw(50)\n        self.assertEqual(self.account.get_balance(), 50)\n\n    def test_deposit_negative_amount_raises_error(self):\n        with self.assertRaises(ValueError):\n            self.account.deposit(-100)\n\n    def test_withdraw_negative_amount_raises_error(self):\n        with self.assertRaises(ValueError):\n            self.account.withdraw(-50)\n\n    def test_withdraw_more_than_balance_raises_error(self):\n        self.account.deposit(100)\n        with self.assertRaises(ValueError):\n            self.account.withdraw(200)\n\n    def test_initial_balance_negative_raises_error(self):\n        with self.assertRaises(ValueError):\n            BankAccount(-100)\n\nif __name__ == '__main__':\n    unittest.main()\n```\n\n### テストケースの実行\n\nCopilotによって生成されるテスト スイートには、プロンプトで指定された各シナリオのテスト ケースが含まれています。\n`BankAccount` クラスの各関数は、さまざまな条件についてテストされます。たとえば、預金や引き出しなどの有効な操作だけでなく、負の金額、利用可能な残高を超える引き出しの試行などのエッジ ケースについてもテストされます。\n\nテスト スイート Copilot 生成されたら、新しいファイル `test_bank_account.py`にコードを追加します。 テストの実行方法を尋ねることができます。\n\n**Prompt:***\"unittest フレームワークを使用して Python でこれらの単体テストをどのように実行しますか？\"*\n\nCopilot により、次の bash コマンドが表示されます。\n\n```bash\npython -m unittest test_bank_account.py\n```\n\nテストを実行すると、その出力がターミナルまたは IDE に表示されます。 すべてのテストに合格したら、`BankAccount` クラスが期待どおりに動作していることを確信できます。\n\n#### スラッシュ コマンド\n\nさらに、Copilotスラッシュ コマンドを使用して、単体テストの完全なスイートを記述するように`/tests`に求めることができます。 IDE の現在のタブでファイルが開かれていることを確認し、 Copilot はそのファイルの単体テストを生成します。\nCopilot生成されるテストはすべてのシナリオに対応しているわけではないため、生成されたコードを常に確認し、必要なテストを追加する必要があります。\n\n> \\[!TIP] 単体テストでまだカバーされていないコード ファイルのテストを記述するように Copilot に依頼した場合は、エディターの隣接するタブで 1 つ以上の既存のテスト ファイルを開くことで、 Copilot に便利なコンテキストを提供できます。\n> Copilot を使用すると、使用するテスト フレームワークを確認でき、既存のテストと一貫性のあるテストを作成する可能性が高くなります。\n\nCopilot では、次のような単体テスト スイートが生成されます。\n\n```python\nimport unittest\nfrom bank_account import BankAccount\n\nclass TestBankAccount(unittest.TestCase):\n    def setUp(self):\n        self.account = BankAccount()\n\n    def test_initial_balance(self):\n        self.assertEqual(self.account.get_balance(), 0)\n```\n\n## を使用して統合テストを記述する Copilot\n\n統合テストは、システムのさまざまなコンポーネントを組み合わせたときに正しく動作することを確認するために不可欠です。 このセクションでは、`BankAccount` クラスを拡張して外部サービス `NotificationSystem` との対話を追加し、またモックを使って、実際の接続を必要とせずにシステムの動作をテストします。 統合テストの目標は、`BankAccount` クラスと `NotificationSystem` サービスの間の対話を検証し、それらが正しく連携していることを確認することです。\n\n### クラスの例: 通知サービスを使用する `BankAccount`\n\n`BankAccount` クラスを更新して、ユーザーに通知を送信する `NotificationSystem` などの外部サービスとの対話を含めましょう。\n`NotificationSystem` は、テストが必要になる統合を表しています。\n\n次のコード スニペットを使って、`BankAccount` ファイルの `bank_account.py` クラスを更新します。\n\n```python\nclass BankAccount:\n    def __init__(self, initial_balance=0, notification_system=None):\n        if initial_balance < 0:\n            raise ValueError(\"Initial balance cannot be negative.\")\n        self.balance = initial_balance\n        self.notification_system = notification_system\n\n    def deposit(self, amount):\n        if amount <= 0:\n            raise ValueError(\"Deposit amount must be positive.\")\n        self.balance += amount\n        if self.notification_system:\n            self.notification_system.notify(f\"Deposited {amount}, new balance: {self.balance}\")\n\n    def withdraw(self, amount):\n        if amount <= 0:\n            raise ValueError(\"Withdrawal amount must be positive.\")\n        if amount > self.balance:\n            raise ValueError(\"Cannot withdraw more than the current balance.\")\n        self.balance -= amount\n\n        if self.notification_system:\n            self.notification_system.notify(f\"Withdrew {amount}, new balance: {self.balance}\")\n\n    def get_balance(self):\n        return self.balance\n```\n\nここでは、Copilot クラスの統合テストをより小さく、より管理しやすいものに書き込むための`BankAccount`の要求を分解します。 これは、より正確で関連性の高いテスト Copilot 生成するのに役立ちます。\n\n**プロンプト:** \"*`deposit` クラスの `BankAccount` 関数の統合テストを記述します。モックを使用して `NotificationSystem` をシミュレートし、それが預金後に正しく呼び出されることを検証します*\"\n\nこのプロンプトでは、いくつかの重要なことを行っています。\n\n* **スコープ**: 単なる単体テストではなく、`deposit` 関数と `NotificationSystem` の対話に焦点を当てて、統合テストを指定しています。\n* **モック**: 明示的にモックを使用して `NotificationSystem` をシミュレートし、その実際の実装に依存せずに外部システムとの対話をテストするように求めています。\n* **検証**: プロンプトでは、預金後に `NotificationSystem` が正しく呼び出されることを検証するように強調しており、各コンポーネントの統合が期待どおりに動作することを確かめています。\n* **具体性**: プロンプトでは、テストするメソッド (`deposit`) とクラス (`BankAccount`) が明確に示されています。\n\n> \\[!TIP]\n> Copilotが無効なテストを生成している場合は、テストする関数の入力と出力の例を指定します。 これにより、関数の期待される動作を Copilot 評価するのに役立ちます。\n\nCopilot では、次のようなテスト スイートが生成されます。\n\n```python\nimport unittest\nfrom unittest.mock import Mock\nfrom bank_account import BankAccount\n\nclass TestBankAccountIntegration(unittest.TestCase):\n    def setUp(self):\n        self.notification_system = Mock()\n\n    def test_deposit_with_notification(self):\n        account = BankAccount(initial_balance=100, notification_system=self.notification_system)\n        account.deposit(50)\n        self.assertEqual(account.get_balance(), 150)\n        self.notification_system.notify.assert_called_once_with(\"Deposited 50, new balance: 150\")\n\nif __name__ == '__main__':\n    unittest.main()\n```\n\n生成されたコードを新しいファイル `test_bank_account_integration.py` に追加します。\n\n### テストケースを改善すること\n\n上記のプロンプトでは、有効な預金が行われたときに `NotificationSystem` が呼び出されることを確認する 1 つのテスト ケースが生成されました。 しかし、預金時にエラーが発生するケースについては考慮されていません。 そのようなシナリオでは、`NotificationSystem` を呼び出すべきではありません。 無効な預金を扱うテスト ケースを追加し、通知システムがトリガーされないことを確認する必要があります。\n\n**プロンプト:** \"*無効な預金額を使用するテスト ケースを追加して、関数が正しい例外を発生させ、`NotificationService` が呼び出されないことを確認します*\"\n\nCopilot では、次のようなテスト ケースが生成されます。\n\n```python\n    def test_deposit_negative_amount_raises_error(self):\n        account = BankAccount(initial_balance=100, notification_system=self.notification_system)\n        with self.assertRaises(ValueError):\n            account.deposit(0)\n        self.notification_system.notify.assert_not_called()\n```\n\n### 改善できる部分を尋ねる\n\n預金の統合機能を検証するテスト ケースを記述したので、このテスト スイート内の改善点を調べる良い機会です。 現在のテストは機能していますが、コード カバレッジを検証し、改善の領域を提案するように Copilot を求めることができます。\n\n**プロンプト:** \"*`BankAccount` クラスと `NotificationSystem` の統合を完全にカバーするためには、どのような追加のテストを含めるべきですか?*\"\n\nCopilot にこのような質問をすれば、見落としているかもしれないテスト ケースを特定することができます。\nこの場合では、有効な預金と無効な預金はテストしましたが、引き出し機能はまだテストしていません。\n\nCopilot では、次のような更新されたテスト スイートが生成されます。\n\n<details>\n  <summary>クリックして、生成された完全なコード例を展開します</summary>\n\n```python\nimport unittest\nfrom unittest.mock import Mock\nfrom bank_account import BankAccount\n\nclass TestBankAccountIntegration(unittest.TestCase):\n    def setUp(self):\n        self.notification_system = Mock()\n\n    def test_deposit_with_notification(self):\n        account = BankAccount(initial_balance=100, notification_system=self.notification_system)\n        account.deposit(50)\n        self.assertEqual(account.get_balance(), 150)\n        self.notification_system.notify.assert_called_once_with(\"Deposited 50, new balance: 150\")\n\n    def test_deposit_negative_amount_raises_error(self):\n        account = BankAccount(initial_balance=100, notification_system=self.notification_system)\n        with self.assertRaises(ValueError):\n            account.deposit(-50)\n        self.notification_system.notify.assert_not_called()\n\n    def test_deposit_zero_amount_raises_error(self):\n        account = BankAccount(initial_balance=100, notification_system=self.notification_system)\n        with self.assertRaises(ValueError):\n            account.deposit(0)\n        self.notification_system.notify.assert_not_called()\n\n    def test_withdraw_with_notification(self):\n        account = BankAccount(initial_balance=100, notification_system=self.notification_system)\n        account.withdraw(30)\n        self.assertEqual(account.get_balance(), 70)\n        self.notification_system.notify.assert_called_once_with(\"Withdrew 30, new balance: 70\")\n\n    def test_withdraw_exceeding_balance_raises_error(self):\n        account = BankAccount(initial_balance=100, notification_system=self.notification_system)\n        with self.assertRaises(ValueError):\n            account.withdraw(150)\n        self.notification_system.notify.assert_not_called()\n\n    def test_withdraw_negative_amount_raises_error(self):\n        account = BankAccount(initial_balance=100, notification_system=self.notification_system)\n        with self.assertRaises(ValueError):\n            account.withdraw(-30)\n        self.notification_system.notify.assert_not_called()\n\n    def test_withdraw_zero_amount_raises_error(self):\n        account = BankAccount(initial_balance=100, notification_system=self.notification_system)\n        with self.assertRaises(ValueError):\n            account.withdraw(0)\n        self.notification_system.notify.assert_not_called()\n\n    def test_initial_negative_balance_raises_error(self):\n        with self.assertRaises(ValueError):\n            BankAccount(initial_balance=-100, notification_system=self.notification_system)\n\nif __name__ == '__main__':\n    unittest.main()\n```\n\n</details>\n\nCopilot が満足のいくテスト スイートを生成したら、次のコマンドを使ってテストを実行し、その結果を確認します。\n\n```bash\npython -m unittest test_bank_account_integration.py\n```\n\n## Copilot Spacesを使用してテストの提案を改善する\n\nCopilot Spaces は、タスク固有のコンテキストを整理して Copilotと共有できる機能です。 これは、ユーザーが受け取る提案の関連性を向上させるのに役立ちます。 プロジェクトに関するより多くのコンテキストを Copilot 提供することで、より良いテスト候補を得ることができます。\n\nたとえば、次のものを含むスペースを作成できます。\n\n* テスト対象のモジュール (`payments.js` など)\n* 現在のテスト スイート (`payments.test.js` など)\n* テスト カバレッジ レポートまたは不足している項目に関するメモ書き\n\nこのスペースでは、次のような Copilot 質問をすることができます。\n\n> `payments.test.js` のロジックを基にすると、`payments.js` にはどのようなテスト ケースが不足していますか?\n\nまたは:\n\n> 既存のテスト スイートの構造に従って、`refund.js` の払戻ロジックの単体テストを記述します。\n\nCopilot Spacesの使用方法の詳細については、「[GitHub コピロット スペースについて](/ja/enterprise-cloud@latest/copilot/concepts/context/spaces)」を参照してください。"}