less than 1 minute read

테스트 케이스

테스트는 코드 작동을 확인하는 루틴. 자동화된 테스트는 테스트 작업이 시스템에서 수행이 됨. tests.py 파일에서 작성됨.

테스트 케이스 작성이 필요한 이유

  • 시간 절약
  • 문제 식별이 아닌 예방
  • 정교한 프로그램 개발 가능

첫 번째 테스트

버그 식별

# polls/tests.py

class QuestionModelTests(TestCase):
    def test_was_published_recently_with_future_question(self):
        """
        was_published_recently() returns False for questions whose pub_date
        is in the future.
        """
        time = timezone.now() + datetime.timedelta(days=30)
        future_question = Question(pub_date=time)
        self.assertIs(future_question.was_published_recently(), False)
  • 우리는 was_published_recently()의 출력이 False가 되었으면 함.

터미널에서 아래와 같이 입력하여 테스트 결과를 확인 가능

python manage.py test polls

View 테스트

장고는 뷰 레벨에서 코드와 상호작용하는 클라이언트 클래스(Client)를 제공

클라이언트 객체 생성

from django.test import Client
client = Client()

참고 링크

Django Part5