About Lesson
Unit tests focus on testing individual pieces of code.
Model Tests
Model tests validate that your models behave as expected.
Example: Testing a Model
View Tests
View tests ensure that your views return the correct responses.
Unit tests focus on testing individual pieces of code.
Model tests validate that your models behave as expected.
from django.test import TestCase
from .models import Post
class PostModelTest(TestCase):
def setUp(self):
self.post = Post.objects.create(title="Test Post", content="Test content.")
def test_post_creation(self):
self.assertEqual(self.post.title, "Test Post")
self.assertEqual(self.post.content, "Test content.")
View tests ensure that your views return the correct responses.
from django.test import TestCase
from django.urls import reverse
class PostListViewTest(TestCase):
def test_post_list_view(self):
response = self.client.get(reverse('post_list'))
self.assertEqual(response.status_code, 200)
self.assertContains(response, "No posts yet.")