Django tutorial, part 5

김수민·2020년 5월 4일
0

Django+uWSGI+nginx

목록 보기
6/9

https://docs.djangoproject.com/ko/3.0/intro/tutorial05/

  • 테스트 자동화의 필요성
  • 테스트 자동화 작성
  • 뷰 테스트

테스트 자동화의 필요성

  • 아무튼 테스트는 중요하다.

첫번째 테스트 작성하기

  • polls 앱에서 Question.was_puslished_recently() 메소드는 Question이 어제 게시된 경우에도 True를 반환하며, Question의 pub_date가 미래로 설정된 경우에도 True를 반환한다.

  • python manage.py shell 을 통해 버그를 확인 해본다.

  • 테스트 모듈을 tests.py 로 생성한다. Django의 테스트 시스템은 해당 모듈을 자동으로 찾는다.

// polls/tests.py


import datetime

from django.test import TestCase
from django.utils import timezone

from .models import Question


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)
  • 테스트 실행
python manage.py test polls
  • 결과

  • Question 모델의 was_published_recently() 메소드의 버그를 수정하고 다시 테스트

뷰 테스트

  • Django는 뷰 레벨에서의 상호작용을 테스트하기위한 Clinet라는 클래스를 지원한다.
  • Client 클래스는 tests.py나 shell에서 사용할 수 있다.

** clinet 테스트는 나중에 좀 더 다루도록 하자.

profile
python developer

0개의 댓글