{"meta":{"title":"단위 테스트 생성","intro":"공동 파일럿 채팅은 함수에 대한 단위 테스트를 생성하는 데 도움이 될 수 있습니다.","product":"GitHub Copilot","breadcrumbs":[{"href":"/ko/copilot","title":"GitHub Copilot"},{"href":"/ko/copilot/tutorials","title":"자습서"},{"href":"/ko/copilot/tutorials/copilot-chat-cookbook","title":"GitHub Copilot Chat 매뉴얼"},{"href":"/ko/copilot/tutorials/copilot-chat-cookbook/testing-code","title":"코드 테스트"},{"href":"/ko/copilot/tutorials/copilot-chat-cookbook/testing-code/generate-unit-tests","title":"단위 테스트 생성"}],"documentType":"article"},"body":"# 단위 테스트 생성\n\n공동 파일럿 채팅은 함수에 대한 단위 테스트를 생성하는 데 도움이 될 수 있습니다.\n\n단위 테스트의 좋은 제품군은 모든 project 성공하는 데 중요합니다. 그러나 이러한 테스트를 작성하는 것은 시간이 오래 걸릴 수 있으며 종종 소홀히 하게 됩니다. 공동 파일럿 채팅은 이러한 테스트를 생성하는 데 도움이 될 수 있습니다.\n\n## 예제 시나리오\n\n데이터베이스를 업데이트하기 전에 데이터의 유효성을 검사하는 Python 함수가 있다고 상상해 보세요. 유효성 검사가 예상대로 수행되도록 단위 테스트를 만들어야 합니다. 공동 파일럿 채팅에게 이러한 테스트를 생성하도록 요청할 수 있습니다.\n\n```python id=validate-price\ndef validate_price(price: float):\n  if price <= 0:\n    raise ValueError(\"Price must be greater than 0\")\n  if price > 1000:\n    raise ValueError(\"Price must be less than or equal to 1000\")\n```\n\n## 예시 프롬프트\n\n```copilot copy prompt ref=validate-price\n/tests Generate unit tests for this function. Validate both success and failure, and include edge cases.\n```\n\n## 예제 응답\n\n> \\[!NOTE] 다음 응답이 예제입니다. 공동 파일럿 채팅 응답은 비결정적이므로 여기에 표시된 응답과 다른 응답을 얻을 수 있습니다.\n\n> \\[!NOTE] 다음 응답이 예제입니다. 공동 파일럿 채팅 응답은 비결정적이므로 여기에 표시된 응답과 다른 응답을 얻을 수 있습니다.\n> 공동 파일럿 채팅에 단위 테스트 생성을 요청하는 것이 처음이면 프로젝트의 단위 테스트 구성을 원하는지 묻는 메시지가 표시될 수 있습니다.\n\n이 예제에서는 이 제안을 거절하는 것으로 가정합니다.\n\n```python\nimport unittest\nfrom generate_unit_tests import validate_price\n\nclass TestValidateProduct(unittest.TestCase):\n  def test_price_zero(self):\n    with self.assertRaises(ValueError) as context:\n      validate_price(0)\n    self.assertEqual(str(context.exception), \"Price must be greater than 0\")\n\n  def test_price_negative(self):\n    with self.assertRaises(ValueError) as context:\n      validate_price(-10)\n    self.assertEqual(str(context.exception), \"Price must be greater than 0\")\n\n  def test_price_above_limit(self):\n    with self.assertRaises(ValueError) as context:\n      validate_price(1500)\n    self.assertEqual(str(context.exception), \"Price must be less than or equal to 1000\")\n\n  def test_price_edge_case_zero(self):\n    with self.assertRaises(ValueError) as context:\n      validate_price(0)\n    self.assertEqual(str(context.exception), \"Price must be greater than 0\")\n\n  def test_price_edge_case_max(self):\n    try:\n      validate_price(1000)\n    except ValueError:\n      self.fail(\"validate_price() raised ValueError unexpectedly!\")\n\nif __name__ == '__main__':\n  unittest.main()\n```\n\n## Copilot는 테스트를 생성하기 위한 전략을 제공한 다음, 테스트 자체를 제공합니다.\n\n* [GitHub Copilot 채팅에 대한 프롬프트 엔지니어링](/ko/copilot/using-github-copilot/prompt-engineering-for-github-copilot)\n* [GitHub 부필로트 사용에 대한 모범 사례](/ko/copilot/using-github-copilot/best-practices-for-using-github-copilot)"}