ChatGPT로 파이썬 Unit Test 초안 작성하기

숲사람·2023년 2월 17일
0

개발생각

목록 보기
5/5
post-thumbnail

ChatGPT로 Unit test 초안 작성하기

호기심에 ChatGpt에게 유닛테스트를 작성해달라고 요청해봤다. 그런데 너무 완벽하게 만들어줘서 놀라웠다. 잘 활용하면 도움이 많이 될것같아 기대가 된다(한편으로는 조금 무섭기도하다).

아래 코드는 최근에 작성했던 Python Class인데 Test Case는 작성하지 않은 상태였다. 해당 클래스에 대한 Test Case작성을 ChatGPT에게 아래와 같이 부탁해 보았다.

Generate test cases about below class using python unittest please.

class summary(str):
    def __init__(self):
        self.header = ''
        self.body = list()

    def __str__(self):
        return self.header + ''.join(self.body)

    def update_header(self, row):
        self.header += row

    def update_body(self, row):
        self.body.append(row)

    def modify_body_line(self, line_num, row):
        self.body[line_num] = row

그랬더니 다음과 같은 답변을 받았다. 내 환경에서 돌려본 결과 1도 수정없이 바로 동작했다ㄷㄷ. 놀라웠다.

Here's a simple test case using the unittest module in Python:

import unittest

class TestSummary(unittest.TestCase):
    def setUp(self):
        self.s = summary()

    def test_update_header(self):
        self.s.update_header("This is the header.")
        self.assertEqual(self.s.header, "This is the header.")

    def test_update_body(self):
        self.s.update_body("This is the first line of the body.")
        self.assertEqual(self.s.body, ["This is the first line of the body."])

    def test_modify_body_line(self):
        self.s.update_body("This is the first line of the body.")
        self.s.modify_body_line(0, "This is a modified line.")
        self.assertEqual(self.s.body, ["This is a modified line."])

    def test_str(self):
        self.s.update_header("This is the header.")
        self.s.update_body("This is the first line of the body.")
        self.assertEqual(str(self.s), "This is the header.This is the first line of the body.")

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

This test case tests the different functions of the summary class and verifies that they behave as expected. The setUp method creates an instance of the summary class that can be used in all of the test methods. The test_update_header method tests the update_header function, the test_update_body method tests the update_body function, the test_modify_body_line method tests the modify_body_line function, and the test_str method tests the str function. Finally, the if name == 'main': statement at the bottom of the file runs the test case when the file is run as a standalone script.

무엇이든 시작이 가장 어렵다. 시작하기 귀찮은 일은 ChatGPT 먼저 초안 작성을 시킨 뒤, 살을 붙이는 방식으로 일하면 생산성이 많이 올라갈것 같다. 특히 유닛테스트 TC작성은 항상 마음의 짐인데, ChatGPT 를 활용하면 보다 쉽게 작성을 시작할 수 있겠다.

우리는 어떤 댓가를 지불했을까

요즘 나는 ChatGPT같은 생성형 AI 서비스가 일상화되면 인간은 어떤것을 잃게 되고 어떤것을 얻게될지 많은 생각들을 한다. 이런저런 아이디어들을 수집중이다(추후 글로 정리해보려고 한다). 우선 이번 사례를 통해, 사람들이 정보를 찾고 얻는 과정에 변화가 어떻게 될지 약간은 가늠해볼 수 있었다. 나의 경우 개발업무 도중 검색 방식은 지금까지는 보통 아래와 같았다.

  1. 어떤 특수한 문제 발생
  2. 검색이 쉽도록 더 일반적인 문제로 재정의 그리고 검색
  3. 유사한 타인의 사례 발견 (Stack Overflow나 Blog를 통해서)
  4. 내 문제에 적용(응용)

이었다면 이제는 ChatGPT로 나의 특수하고 구체적인 문제에 대한 직접적인 해답을 바로 얻는 방식이다. 어떤 조수가 내 문제에 대해서만 특별한 솔루션을 제안하는것과 같다. 이런 방식의 정보 탐색이 일상화가 되면 우리 인간은 분명 얻는것도 있고 잃는것도 있을것이다. 간단하게 한번 생각해보았다.

  • 얻는 것
    정보 접근성 및 생산성 향상

  • 잃는 것
    기존의 구글링은, 추상화 능력, 응용력을 향상시키기 위한 훈련과도 같다. 하지만 ChatGPT 로 정확히 내 문제에대한 해결책을 누군가 대신 해주다보면, 분명 인간은 어떤 능력은 더이상 필요없게 될것같다. 검색이 쉽도록 문제를 더 일반적으로 재정의 하는과정을 통해 일반화능력 추상화 범주화 능력이 길러진다. 또한 다른사람의 특수한 사례를 내 문제에 적용하는과정을 통해 응용력이 길러진다. 이러한 감각과 능력을 점차 상실하게 되지 않을까? 인간이 네비게이션을 사용하면서 길찾기 능력이 감퇴한 것처럼.

profile
기록 & 정리 아카이브 용도 (보다 완성된 글은 http://soopsaram.com/documentudy)

0개의 댓글