์ข์ ๋จ์ ํ ์คํธ ์ ํ๊ตฐ์ ๋ชจ๋ ํ๋ก์ ํธ์ ์ฑ๊ณต์ ๋งค์ฐ ์ค์ํฉ๋๋ค. ๊ทธ๋ฌ๋ ์ด๋ฌํ ํ ์คํธ๋ฅผ ์์ฑํ๋ ๊ฒ์ ์๊ฐ์ด ์ค๋ ๊ฑธ๋ฆด ์ ์์ผ๋ฉฐ ์ข ์ข ์ํํ ํ๊ฒ ๋ฉ๋๋ค. Copilot Chat์ ์ด๋ฌํ ํ ์คํธ๋ฅผ ์์ฑํ๋ ๋ฐ ๋์์ด ๋ ์ ์์ต๋๋ค.
์์ ์๋๋ฆฌ์ค
๋ฐ์ดํฐ๋ฒ ์ด์ค๋ฅผ ์ ๋ฐ์ดํธํ๊ธฐ ์ ์ ๋ฐ์ดํฐ์ ์ ํจ์ฑ์ ๊ฒ์ฌํ๋ Python ํจ์๊ฐ ์๋ค๊ณ ์์ํด ๋ณด์ธ์. ์ ํจ์ฑ ๊ฒ์ฌ๊ฐ ์์๋๋ก ์ํ๋๋๋ก ๋จ์ ํ ์คํธ๋ฅผ ๋ง๋ค์ด์ผ ํฉ๋๋ค. Copilot Chat์๊ฒ ์ด๋ฌํ ํ ์คํธ๋ฅผ ์์ฑํ๋๋ก ์์ฒญํ ์ ์์ต๋๋ค.
def validate_price(price: float):
if price <= 0:
raise ValueError("Price must be greater than 0")
if price > 1000:
raise ValueError("Price must be less than or equal to 1000")
์์ ํ๋กฌํํธ
/tests Generate unit tests for this function. Validate both success and failure, and include edge cases.
์์ ์๋ต
์ฐธ๊ณ ํญ๋ชฉ
๋ค์ ์๋ต์ด ์์ ์ ๋๋ค. Copilot Chat ์๋ต์ ๋น๊ฒฐ์ ์ ์ด๋ฏ๋ก ์ฌ๊ธฐ์ ํ์๋ ์๋ต๊ณผ ๋ค๋ฅธ ์๋ต์ ์ป์ ์ ์์ต๋๋ค.
์ฒ์์ผ๋ก Copilot Chat์๊ฒ ๋จ์ ํ ์คํธ๋ฅผ ์์ฑํ๋๋ก ์์ฒญํ๋ ๊ฒฝ์ฐ ํ๋ก์ ํธ์ ๋ํ ๋จ์ ํ ์คํธ๋ฅผ ๊ตฌ์ฑํ ์ง ๋ฌป๋ ๋ฉ์์ง๊ฐ ํ์๋ ์ ์์ต๋๋ค. ์ด ์์ ์์๋ ์ด ์ ์์ ๊ฑฐ์ ํ๋ ๊ฒ์ผ๋ก ๊ฐ์ ํฉ๋๋ค.
Copilot๋ ํ ์คํธ๋ฅผ ์์ฑํ๊ธฐ ์ํ ์ ๋ต์ ์ ๊ณตํ ๋ค์, ํ ์คํธ ์์ฒด๋ฅผ ์ ๊ณตํฉ๋๋ค.
import unittest
from generate_unit_tests import validate_price
class TestValidateProduct(unittest.TestCase):
def test_price_zero(self):
with self.assertRaises(ValueError) as context:
validate_price(0)
self.assertEqual(str(context.exception), "Price must be greater than 0")
def test_price_negative(self):
with self.assertRaises(ValueError) as context:
validate_price(-10)
self.assertEqual(str(context.exception), "Price must be greater than 0")
def test_price_above_limit(self):
with self.assertRaises(ValueError) as context:
validate_price(1500)
self.assertEqual(str(context.exception), "Price must be less than or equal to 1000")
def test_price_edge_case_zero(self):
with self.assertRaises(ValueError) as context:
validate_price(0)
self.assertEqual(str(context.exception), "Price must be greater than 0")
def test_price_edge_case_max(self):
try:
validate_price(1000)
except ValueError:
self.fail("validate_price() raised ValueError unexpectedly!")
if __name__ == '__main__':
unittest.main()