참고
Django Documentation: Testing in Django
Django Tutorial Part 10: Testing a Django web application
실습 저장소
https://github.com/JisunParkRea/my_djangotube
models.py 테스트
from django.test import TestCase
from django.contrib.auth.models import User
from video.models import Video
class VideoModelTest(TestCase):
@classmethod
def setUpTestData(cls):
# Set up non-modified objects used by all test methods
test_user = User.objects.create_user(username='test_user', email='test_user@example.com', password='test_password')
Video.objects.create(author=test_user, title='코로나 국민예방수칙', category='info', video_key='G7rBBWeunHM')
def test_title_label(self):
video = Video.objects.get(id=1)
field_label = video._meta.get_field('title').verbose_name
self.assertEquals(field_label, 'title')
def test_title_max_length(self):
video = Video.objects.get(id=1)
max_length = video._meta.get_field('title').max_length
self.assertEquals(max_length, 100)
def test_default_count_likes_user_is_zero(self):
video = Video.objects.get(id=1)
default_count_likes_user = video.count_likes_user()
self.assertEquals(default_count_likes_user, 0)
def test_object_name_is_title(self):
video = Video.objects.get(id=1)
expected_object_name = video.title
self.assertEquals(expected_object_name, str(video.title))
views.py 테스트
- LOGIN_URL = '/video/login/' 을 추가할 수 있었다
from django.test import TestCase
from django.urls import reverse
from django.contrib.auth.mixins import LoginRequiredMixin
from django.contrib.auth.models import User
from video.models import Video
from video.views import my_video
class VideoListViewTest(TestCase):
@classmethod
def setUpTestData(cls):
# Set up non-modified objects used by all test methods
test_user = User.objects.create_user(username='test_user', email='test_user@example.com', password='test_password')
Video.objects.create(author=test_user, title='코로나 국민예방수칙', category='info', video_key='G7rBBWeunHM')
def test_view_url_exists_at_desired_location(self):
response = self.client.get('/video/')
self.assertEqual(response.status_code, 200)
def test_view_url_accessible_by_name(self):
response = self.client.get(reverse('video_list'))
self.assertEqual(response.status_code, 200)
def test_view_uses_correct_template(self):
response = self.client.get(reverse('video_list'))
self.assertEqual(response.status_code, 200)
self.assertTemplateUsed(response, 'video/video_list.html')
class MyVideoListViewTest(TestCase):
def setUp(self):
# Create user
test_user = User.objects.create_user(username='test_user', email='test_user@example.com', password='test_password')
test_user.save()
# Create video
Video.objects.create(author=test_user, title='코로나 국민예방수칙', category='info', video_key='G7rBBWeunHM')
def test_redirect_if_not_logged_in(self):
response = self.client.get(reverse('my_video'))
self.assertRedirects(response, '/video/login/?next=/video/myVideo/')
def test_logged_in_uses_correct_template(self):
self.client.login(username='test_user', password='test_password')
response = self.client.get(reverse('my_video'))
# Check our user is logged in
self.assertEqual(str(response.context['user']), 'test_user')
# Check that we got a response "success"
self.assertEqual(response.status_code, 200)
# Check we used correct template
self.assertTemplateUsed(response, 'video/my_video.html')