# コードの変更に一致するように単体テストを更新する

Copilot Chat は、テストの更新に役立ちます。

コードを変更するときは、新しい動作を検証し、新しいコードによって導入されたバグをキャッチするようにテストを更新することが重要です。
Copilot Chat は、コードの変更に合わせてテストをすばやく更新し、テスト スイートが実装と同期していることを確認するのに役立ちます。

## サンプル シナリオ

特定の購入金額の割引を決定する Python 関数 ( `calculate_discount`) があるとします。 元のコードでは、100 ドルを超える金額に対して 10% 割引が適用されます。 関数のロジックを変更して、150 ドルを超える価格のみが 10% 割引を受け、200 ドルを超える金額に対して 20% 割引が適用されるようになりました。

### オリジナル コピー

元のコードでは、100 ドルを超えて価格を購入すると、10% 割引が適用されます。

```python
def calculate_discount(amount: float) -> float:
    if amount > 100:
        return amount * 0.1  # 10% discount
    return 0.0
```

### 更新されたコード

変更されたコードでは、$150 を超える金額のみが 10% 割引を受け、200 ドルを超える金額は 20% 割引を受けるようになりました。

```python id=calculate_discount
def calculate_discount(amount: float) -> float:
    if amount > 200:
        return amount * 0.2  
    elif amount > 150:
        return amount * 0.1
    return 0.0
```

### 既存のテスト

この例では、元のコード用に記述された既存のテストを示します。

```python
import unittest
from discount import calculate_discount

class TestCalculateDiscount(unittest.TestCase):
    def test_discount_above_100(self):
        # $140 should get a $14 discount (10%)
        self.assertAlmostEqual(calculate_discount(140), 14.0)
    
    def test_discount_below_100(self):
        # $80 should get no discount
        self.assertEqual(calculate_discount(80), 0.0)

    def test_discount_exactly_100(self):
        # $100 is not above 100, so no discount
        self.assertEqual(calculate_discount(100), 0.0)

if __name__ == '__main__':
    unittest.main()
```

## プロンプトの例

ロジックの変更の影響を受けるテストを特定して更新するように Copilot Chat に依頼できます。

```copilot
Given the update to the `calculate_discount` function, update the unit tests that may fail or become outdated as a result.
```

## 応答の例

> \[!NOTE] 次の応答は例です。 Copilot Chat の応答は非決定的であるため、ここで示したものとは異なる応答が返される可能性があります。

Copilot Chat は、コードと既存のテストを分析し、コードの変更後に失敗するテストまたは現在誤解を招くテストを識別します。

たとえば、 Copilot Chat では次のことが説明されています。

* `test_discount_above_100`はこれから**失敗**します
* `test_discount_below_100`と`test_discount_exactly_100`は合格しますが、10%割引のしきい値が$100ではなく$150になったため、**誤解を招く**ようになりました。

さらに、 Copilot Chat は、200 ドルを超える金額の新しい 20% 割引レベルなど、 **不足しているテスト 対象**範囲を識別します。

Copilot Chat は、更新された一連のテストを提案します。

### 更新されたテスト

```python
import unittest
from discount import calculate_discount

class TestCalculateDiscount(unittest.TestCase):
    def test_discount_above_200(self):
        # $250 should get a $50 discount (20%)
        self.assertAlmostEqual(calculate_discount(250), 50.0)

    def test_discount_above_150(self):
        # $175 should get a $17.50 discount (10%)
        self.assertAlmostEqual(calculate_discount(175), 17.5)

    def test_discount_at_or_below_150(self):
        # $150 is not above 150, so no discount
        self.assertEqual(calculate_discount(150), 0.0)
        # $80 should get no discount
        self.assertEqual(calculate_discount(80), 0.0)

if __name__ == '__main__':
    unittest.main()
```

## 詳細については、次を参照してください。

* [GitHub Copilot Chat のプロンプト エンジニアリング](/ja/enterprise-cloud@latest/copilot/concepts/prompting/prompt-engineering)
* [GitHub Copilot の使用に関するベスト プラクティス](/ja/enterprise-cloud@latest/copilot/get-started/best-practices)