Skip to main content

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

부조종사 채팅은(는) 테스트 업데이트에 도움이 될 수 있습니다.

코드를 변경할 때는 테스트를 업데이트하여 새 동작을 확인하고 새 코드에서 도입한 버그를 catch하는 것이 중요합니다. 부조종사 채팅를 사용하면 코드 변경 내용과 일치하도록 테스트를 빠르게 업데이트하여 테스트 스위트가 구현과 동기화되도록 유지할 수 있습니다.

예제 시나리오

지정된 구매 금액에 대한 할인을 결정하는 Python 함수 calculate_discount가 있다고 상상해 보십시오. 원래 코드에서는 $100 이상의 금액에 대해 10% 할인을 받습니다. 함수 논리를 변경하여 $150 이상의 가격만 10% 할인을 받을 수 있으며, 이제 $200 이상의 금액에 대해 20개의% 할인이 제공됩니다.

원래 코드

원래 코드에서 $ 100 이상의 구매 가격은 10% 할인을 받습니다.

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

업데이트된 코드

변경된 코드에서 $150 이상의 금액만 10% 할인을 받고$ 200 이상의 금액은 이제 20% 할인을 받습니다.

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

기존 테스트

이 예제에서는 원래 코드에 대해 작성된 기존 테스트를 보여 줍니다.

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()

예시 프롬프트

부조종사 채팅에게 논리 변경의 영향을 받는 테스트를 식별하고 업데이트하도록 요청할 수 있습니다.

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

응답 예제

참고 항목

다음 응답이 예제입니다. 부조종사 채팅 응답은 비결정적이므로 여기에 표시된 응답과 다른 응답을 얻을 수 있습니다.

부조종사 채팅은(는) 코드 및 기존 테스트를 분석하고, 코드 변경 후 실패하거나 오해의 소지가 있는 테스트를 식별합니다.

예를 들어 부조종사 채팅는 다음을 설명합니다. * test_discount_above_100 이제 실패합니다. * test_discount_below_100 10 test_discount_exactly_100% 할인에 대한 임계 값이 이제 $ 150이 아니라 $ 100이므로 통과하지만 지금은 오해의 소지가 있습니다.

또한 부조종사 채팅는 $200 이상의 금액에 대한 새로운 20% 할인 계층과 같은 누락된 테스트 범위를 식별합니다.

부조종사 채팅 업데이트된 테스트 집합을 제안합니다.

업데이트된 테스트

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()

추가 읽기