μλνλ ν
μ€νΈλ μμμ shell
μ μ¬μ©ν΄ λ©μλμ λμμ κ²μ¬νκ±°λ λ°μ΄ν°λ₯Ό μ
λ ₯ν΄μ ν
μ€νΈ νκ²κ³Ό λ€λ₯΄μ§ μλ€. μ°¨μ΄μ μ ν
μ€νΈ μμ
μ΄ μμ€ν
μμ μνλλ€λ μ μ΄λ€. νλ² ν
μ€νΈ μΈνΈλ₯Ό μμ±ν νμλ μ±μ λ³κ²½ν λ μλ ν
μ€νΈλ₯Ό μννμ§ μμλ μλ μλλλ‘ μ½λκ° μλνλμ§ νμΈν μ μλ€.
μλνλ ν
μ€νΈλ₯Ό ν΅ν΄ μκ°μ μ μ½ν μ μλ€. ν
μ€νΈλ₯Ό μμ±νλ μμ
μ μ΄ν리μΌμ΄μ
μ μλμΌλ‘ ν
μ€νΈνκ±°λ μλ‘ λ°κ²¬λ λ¬Έμ μ μμΈμ νμΈνλ λ° λ§μ μκ°μ ν¬μνλ κ²λ³΄λ€ ν¨μ¬ λ ν¨κ³Όμ μ
λλ€.
λ¬Έμ λ₯Ό μλ³νλ κ²μ΄ μλλΌ μλ°©ν μ μλ€.
μ ν리μΌμ΄μ
ν
μ€νΈλ μΌλ°μ μΌλ‘ <app_name>/tests.py
νμΌμ μλ€. ν
μ€νΈ μμ€ν
μ test λ‘ μμνλ λ©μλλ₯Ό μλμΌλ‘ μ°Ύλλ€.
Model Test
Copy # polls/models.py
class Question(models.Model):
question_text = models.CharField(max_length=200)
pub_date = models.DateTimeField('date published')
def __str__(self):
return '%s >> %s' % (self.question_text, self.pub_date)
def was_published_recently(self):
return self.pub_date >= timezone.now() - datetime.timedelta(days=1)
Copy # polls/test.py
import datetime
from django.utils import timezone
from django.test import TestCase
from .models import Question
# Create your tests here.
class QuestionMdoelTests(TestCase):
def test_was_published_recently_with_future_question(self):
time = timezone.now() + datetime.timedelta(days=30)
future_question = Question(pub_date=time)
self.assertIs(future_question.was_published_recently(),False)
λ€μμ κ°λ¨ν μμμ΄λ€. μ§κΈ μκ°λ³΄λ€ 30μΌ μ΄νμ Questionμ μμ±ν λ€ μ΅κ·Όμ μμ±νκ² λ§λμ§ νμΈνλ κ²μ΄λ€.
Copy $ python manage.py test <app_name>
Copy $ python manage.py test polls
Creating test database for alias 'default'...
System check identified no issues (0 silenced).
F
======================================================================
FAIL: test_was_published_recently_with_future_question (polls.tests.QuestionMdoelTests)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/Users/jeongdaye/Documents/study/test_app/mysite2/polls/tests.py", line 13, in test_was_published_recently_with_future_question
self.assertIs(future_question.was_published_recently(),False)
AssertionError: True is not False
----------------------------------------------------------------------
Ran 1 test in 0.001s
FAILED (failures=1)
Destroying test database for alias 'default'...
μ°λ¦¬λ Falseκ° returnλκΈ°λ₯Ό λ°λΌλλ° Trueκ° λ°νλλ€λ κ²μ λ°κ²¬ν μ μλ€.
Copy # Create your models here.
class Question(models.Model):
question_text = models.CharField(max_length=200)
pub_date = models.DateTimeField('date published')
def __str__(self):
return '%s >> %s' % (self.question_text, self.pub_date)
def was_published_recently(self):
now = timezone.now()
return now - datetime.timedelta(days=1) <= self.pub_date <= now
λ€μκ³Ό κ°μ΄ was_published_recently()
λ₯Ό μμ ν΄μ£Όκ³ ν
μ€νΈ λͺ
λ Ήμ΄λ₯Ό μ€νν΄λ³΄λ©΄ ν
μ€νΈλ₯Ό ν΅κ³Όν κ²μ νμΈν μ μλ€.
Copy Creating test database for alias 'default'...
System check identified no issues (0 silenced).
.
----------------------------------------------------------------------
Ran 1 test in 0.000s
OK
Destroying test database for alias 'default'...
View Test
Shellμ ν
μ€νΈ νκ²½ ꡬμ±νκΈ°
Copy $ python manage.py shell
Python 3.7.2 (default, Mar 5 2019, 16:08:31)
[Clang 10.0.0 (clang-1000.10.44.4)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>>
Copy >>> from django.test.utils import setup_test_environment
>>> setup_test_environment()
μ΄ λ©μλλ ν
μ€νΈ λ°μ΄ν°λ² μ΄μ€λ₯Ό μ€μ νμ§ μκΈ° λλ¬Έμ νμ¬ μ¬μ©μ€μΈ λ°μ΄ν°λ² μ΄μ€ μμμ λκ²λλ©° κ²°κ³Όλ λ°μ΄ν°λ² μ΄μ€μ λ°μ΄ν°μ λ°λΌ λ€λ₯΄κ² λμ¨λ€.
Copy >>> from django.test import Client
>>> client = Client()
ν
μ€νΈ ν΄λΌμ΄μΈνΈλ₯Ό μμ±νμ¬ μμ
μ μνν μ μλ€.
Copy >>> from django.test.utils import setup_test_environment
>>> setup_test_environment()
>>> from django.test import Client
>>> client = Client()
>>> 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/2/">Are you happy?</a></li>\n \n <li><a href="/polls/1/">What's your name?</a></li>\n \n </ul>\n'
>>> response.context['latest_question_list']
<QuerySet [<Question: Are you happy? >> 2019-04-05 05:54:39>, <Question: What's your name? >> 2019-04-04 06:21:55.323284>]>
>>>
Copy def create_question(question_text, days):
time = timezone.now() + datetime.timedelta(days=days)
return Question.objects.create(question_text=question_text, pub_date=time)
class QuestionIndexViewTests(TestCase):
def test_no_questions(self):
response = self.client.get(reverse('polls:index'))
self.assertEqual(response.status_code, 200)
self.assertContains(response, "No polls are available.")
self.assertQuerysetEqual(response.context['latest_question_list'], [])
def test_past_question(self):
create_question(question_text="Past question.", days=-30)
response = self.client.get(reverse('polls:index'))
self.assertQuerysetEqual(
response.context['latest_question_list'],
['<Question: Past question.>']
)
def test_future_question(self):
create_question(question_text="Future question.", days=30)
response = self.client.get(reverse('polls:index'))
self.assertContains(response, "No polls are available.")
self.assertQuerysetEqual(response.context['latest_question_list'], [])
def test_future_question_and_past_question(self):
create_question(question_text="Past question.", days=-30)
create_question(question_text="Future question.", days=30)
response = self.client.get(reverse('polls:index'))
self.assertQuerysetEqual(
response.context['latest_question_list'],
['<Question: Past question.>']
)
def test_two_past_questions(self):
create_question(question_text="Past question 1.", days=-30)
create_question(question_text="Past question 2.", days=-5)
response = self.client.get(reverse('polls:index'))
self.assertQuerysetEqual(
response.context['latest_question_list'],
['<Question: Past question 2.>', '<Question: Past question 1.>']
)
class QuestionDetailViewTests(TestCase):
def test_future_question(self):
future_question = create_question(question_text='Future question.', days=5)
url = reverse('polls:detail', args=(future_question.id,))
response = self.client.get(url)
self.assertEqual(response.status_code, 404)
def test_past_question(self):
past_question = create_question(question_text='Past Question.', days=-5)
url = reverse('polls:detail', args=(past_question.id,))
response = self.client.get(url)
self.assertContains(response, past_question.question_text)
λ€μκ³Ό κ°μ΄ Viewμ λν΄μλ ν
μ€νΈ μ½λλ₯Ό μμ±ν μ μλ€. ν
μ€νΈλ₯Ό ν λλ λ§μ΄ ν μλ‘ μ’λ€.