# 코드 변경 내용과 일치하도록 단위 테스트 업데이트

공동 파일럿 채팅은(는) 테스트를 업데이트하는 데 도움을 줄 수 있습니다.

코드를 변경할 때는 테스트를 업데이트하여 새 동작을 확인하고 새 코드에서 도입한 버그를 catch하는 것이 중요합니다. 공동 파일럿 채팅은(는) 코드 변경 내용에 맞게 테스트를 빠르게 업데이트하여 테스트 스위트가 구현과 계속 동기 상태를 유지하도록 도와줍니다.

## 예제 시나리오

지정된 구매 금액에 대한 할인을 결정하는 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
Given the update to the `calculate_discount` function, update the unit tests that may fail or become outdated as a result.
```

## 응답 예제

> \[!NOTE] 다음 응답이 예제입니다. 공동 파일럿 채팅 응답은 비결정적이므로 여기에 표시된 응답과 다른 응답을 얻을 수 있습니다.

> \[!NOTE] 다음 응답이 예제입니다. 공동 파일럿 채팅 응답은 비결정적이므로 여기에 표시된 응답과 다른 응답을 얻을 수 있습니다.

공동 파일럿 채팅은(는) 코드와 기존 테스트를 분석하여 코드 변경 이후 어떤 테스트가 실패하거나 이제 잘못된 결과를 유도하는지 식별합니다.

* 예를 들어, 공동 파일럿 채팅은(는) 다음을 설명합니다:
* `test_discount_above_100` 이제 **실패합니다.**

  ```
          `test_discount_below_100` 및 `test_discount_exactly_100`은(는) 통과하지만, 10% 할인 임계값이 이제 100달러가 아닌 150달러이기 때문에 지금은 **오해의 소지가 있는** 테스트입니다.
  ```

또한 공동 파일럿 채팅은(는) 200달러 초과 금액에 대한 새 20% 할인 구간과 같이 **누락된 테스트 범위**도 식별합니다.

### 공동 파일럿 채팅은(는) 업데이트된 테스트 집합을 제안합니다.

```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 채팅에 대한 프롬프트 엔지니어링](/ko/copilot/using-github-copilot/prompt-engineering-for-github-copilot)
* [GitHub 부필로트 사용에 대한 모범 사례](/ko/copilot/using-github-copilot/best-practices-for-using-github-copilot)