1. python manage.py shell 로 쉘에 들어감
2. 아래와 같이 작성하여 template renderer 설치
>>> from django.test.utils import setup_test_environment
>>> setup_test_environment()
3. 아래와와 같이 작성하여 테스트 클라이언트 클래스 가져오기
>>> from django.test import Client
>>> # create an instance of the client for our use
>>> client = Client()
4. 아래 명령을 이용하여 test.py 코드를 작성하지 않고도 테스트를 장고 테스트 클라이언트를 통해서 수행해 볼 수 있음
>>> # get a response from '/'
>>> response = client.get('/')
Not Found: /
>>> # we should expect a 404 from that address; if you instead see an
>>> # "Invalid HTTP_HOST header" error and a 400 response, you probably
>>> # omitted the setup_test_environment() call described earlier.
>>> response.status_code
404
>>> # on the other hand we should expect to find something at '/polls/'
>>> # we'll use 'reverse()' rather than a hardcoded URL
>>> from django.urls import reverse
>>> response = client.get(reverse('polls:index'))
>>> response.status_code
200
>>> response.content
b'\n <ul>\n \n <li><a href="/polls/1/">What's up?</a></li>\n \n </ul>\n\n'
>>> response.context['latest_question_list']
<QuerySet [<Question: What's up?>]>
- response.status_code 가 200이면 정상 작동하는 것임
728x90
'Django' 카테고리의 다른 글
Django tutorial (0) | 2022.01.20 |
---|