from django.test import TestCase, Client
from django.urls import reverse
from posts_app.models import CustomUser, Post
TestCase
: Django's testing class that helps you create unit tests for views, models, and other components of your application.Client
: Django's test client used to simulate requests to your application, making it possible to test your views and responses.reverse
: A function to get the URL of a view by its name, instead of hardcoding the URL.CustomUser
andPost
: The models you're testing in your views.
def setUp(self):
self.client = Client()
self.user = CustomUser.objects.create_user(
username="testuser",
email="[email protected]",
password="password123",
first_name="Test",
last_name="User"
)
self.post = Post.objects.create(
user=self.user,
content="This is a test post content",
categories="Test Category",
visibility="public"
)
setUp
: This method runs before each test method. It's used to prepare the necessary test data.self.client = Client()
: Initializes the test client that will be used to simulate HTTP requests.self.user = CustomUser.objects.create_user(...)
: Creates a new user in the test database with specific details.self.post = Post.objects.create(...)
: Creates a newPost
associated with the test user.
def test_home_page_view(self):
response = self.client.get(reverse("home"))
self.assertEqual(response.status_code, 200)
self.assertTemplateUsed(response, "posts_app/home.html")
- This test checks if the home page view works as expected.
self.client.get(reverse("home"))
: Sends a GET request to the home page URL.self.assertEqual(response.status_code, 200)
: Asserts that the response status code is200 OK
.self.assertTemplateUsed(response, "posts_app/home.html")
: Asserts that the template used to render the page is"posts_app/home.html"
.
def test_users_list_view(self):
response = self.client.get(reverse("users-list"))
self.assertEqual(response.status_code, 200)
self.assertTemplateUsed(response, "posts_app/users.html")
self.assertIn(self.user, response.context["users"])
- This test checks if the users list view works correctly.
self.client.get(reverse("users-list"))
: Sends a GET request to the users list view URL.self.assertEqual(response.status_code, 200)
: Asserts the status code is 200.self.assertTemplateUsed(response, "posts_app/users.html")
: Checks if the correct template is used.self.assertIn(self.user, response.context["users"])
: Verifies that the createdself.user
is included in theusers
context passed to the template.
def test_posts_list_view(self):
response = self.client.get(reverse("posts-list"))
self.assertEqual(response.status_code, 200)
self.assertTemplateUsed(response, "posts_app/posts.html")
self.assertIn(self.post, response.context["posts"])
- This test checks if the posts list view works correctly.
- Similar to the previous test, it checks the status code, template, and verifies that the created
self.post
appears in theposts
context.
def test_user_detail_view(self):
response = self.client.get(reverse("user-details", kwargs={"username": self.user.username}))
self.assertEqual(response.status_code, 200)
self.assertTemplateUsed(response, "posts_app/profile.html")
self.assertEqual(response.context["user"], self.user)
- This test checks if the user detail view works correctly.
reverse("user-details", kwargs={"username": self.user.username})
: Generates the URL for the user details view for the given username.self.assertEqual(response.context["user"], self.user)
: Verifies that theuser
context in the response corresponds to the createdself.user
.
def test_post_detail_view(self):
response = self.client.get(reverse("post-details", kwargs={"pk": self.post.pk}))
self.assertEqual(response.status_code, 200)
self.assertTemplateUsed(response, "posts_app/post_details.html")
self.assertEqual(response.context["post"], self.post)
- This test checks if the post detail view works correctly.
reverse("post-details", kwargs={"pk": self.post.pk})
: Generates the URL for the post details view based on the primary key (pk
) ofself.post
.
def test_user_posts_view(self):
response = self.client.get(reverse("user-posts", kwargs={"username": self.user.username}))
self.assertEqual(response.status_code, 200)
self.assertTemplateUsed(response, "posts_app/user_posts.html")
self.assertIn(self.post, response.context["posts"])
- This test checks if the user posts view works correctly.
- Similar to the previous tests, it checks the URL, status code, template, and the presence of the created post in the context.
def test_user_register_view(self):
response = self.client.get(reverse("register"))
self.assertEqual(response.status_code, 200)
self.assertTemplateUsed(response, "posts_app/register.html")
- This test checks if the user registration page works correctly.
self.client.get(reverse("register"))
: Sends a GET request to the registration page.- It asserts the status code and checks if the correct template is used.
def test_user_login_view(self):
response = self.client.post(reverse("login"), data={"username": "testuser", "password": "password123"})
self.assertEqual(response.status_code, 302)
self.assertRedirects(response, reverse("home"))
- This test checks if the user login view works correctly.
- It sends a POST request with the login credentials and asserts the response status code is
302
(redirect). self.assertRedirects(response, reverse("home"))
: Verifies that the user is redirected to the home page after successful login.
def test_user_logout_view(self):
self.client.login(username="testuser", password="password123")
response = self.client.get(reverse("logout"))
self.assertRedirects(response, reverse("home"))
- This test checks if the user logout view works correctly.
- The client first logs in using
self.client.login(...)
. - It then sends a GET request to the logout URL and checks that the response redirects to the home page.
def test_post_create_view(self):
self.client.login(username="testuser", password="password123")
response = self.client.post(
reverse("new-post"),
data={
"content": "Another test post",
"categories": "Test Category",
"visibility": "public",
}
)
self.assertEqual(response.status_code, 200)
- This test checks if creating a new post works for a logged-in user.
- It sends a POST request to the "new-post" URL with valid data and asserts the status code.
def test_post_create_without_login(self):
response = self.client.post(
reverse("new-post"),
data={
"content": "Another test post",
"categories": "Test Category",
"visibility": "public",
}
)
self.assertEqual(response.status_code, 302)
- This test checks that a user must be logged in to create a post.
- It sends a POST request without logging in and expects a
302
(redirect) response, usually indicating a redirect to the login page.
def test_post_update_view(self):
self.client.login(username="testuser", password="password123")
response = self.client.post(
reverse("post-update", kwargs={"pk": self.post.pk}),
data={
"content": "Updated content tha is longer than 20 chars",
"categories": "Updated data",
"visibility": "private",
}
)
self.assertEqual(response.status_code, 302)
self.post.refresh_from_db()
self.assertEqual(self.post.content, "Updated content tha is longer than 20 chars")
- This test checks if updating a post works correctly.
- It logs in the user and sends a POST request to update the post.
- Afterward, it asserts the post has been updated in the database.
def test_post_delete_view(self):
self.client.login(username="testuser", password="password123")
response = self.client.post(reverse("post-delete", kwargs={"pk": self.post.pk}))
self.assertRedirects(response, reverse("user-posts", kwargs={"username": "testuser"}))
self.assertFalse(Post.objects.filter(pk=self.post.pk).exists())
- This test checks if deleting a post works correctly.
- It logs in the user and sends a POST request to delete the post.
- It then checks if the post has been deleted and if the user is redirected to their post list page.
-
client
is a test utility provided by Django'sTestCase
class. It acts like a dummy web browser that sends requests to the server and receives responses. The test client allows you to simulate GET, POST, and other HTTP methods to test views and their functionality. -
It simulates the process of making a request and interacting with the server as if it were an actual user visiting your site. It is useful for testing views, forms, and the overall response of the Django application without the need for a real browser or user interaction.