Here is my test:
from django.urls import reverse
from django.test import TestCase
from django.urls import resolve
from django.contrib.auth.models import User
from ..views import index, board_topics, new_topic
from ..models import Board, Topic, Post
from ..forms import NewTopicForm
class NewTopicTests(TestCase):
def setUp(self):
self.board = Board.objects.create(name="Python", description="Everything related to Python")
self.user = User.objects.create_user(username='jane', email='jane@doe.com', password='123')
def test_csrf(self):
url = reverse('new_topic', kwargs={'id': 1})
response = self.client.get(url)
self.assertContains(response, 'csrfmiddlewaretoken') <- I think this is the culprit
def test_new_topic_valid_post_data(self):
url = reverse('new_topic', kwargs={'id': 1})
data = {
'subject': 'Test title',
'message': 'Lorem ipsum dolor sit amet'
}
response = self.client.post(url, data)
self.assertTrue(Topic.objects.exists())
self.assertTrue(Post.objects.exists())
def test_new_topic_invalid_post_data(self):
'''
Invalid post data should not redirect
The expected behavior is to show the form again with validation errors
'''
url = reverse('new_topic', kwargs={'id': 1})
response = self.client.post(url, {})
self.assertEqual(response.status_code, 200)
def test_new_topic_invalid_post_data_empty_fields(self):
'''
Invalid post data should not redirect
The expected behavior is to show the form again with validation errors
'''
url = reverse('new_topic', kwargs={'id': 1})
data = {
'subject': '',
'message': ''
}
And here is the output to Terminal:
Found 4 test(s).
Creating test database for alias 'default'...
System check identified no issues (0 silenced).
F..F
======================================================================
FAIL: test_csrf (boards.tests.test_new_topic_tests.NewTopicTests.test_csrf)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/Users/mariacam/Python-Development/django-boards/django_boards/boards/tests/test_new_topic_tests.py", line 18, in test_csrf
self.assertContains(response, 'csrfmiddlewaretoken')
File "/Users/mariacam/.pyenv/versions/3.12.5/lib/python3.12/site-packages/django/test/testcases.py", line 609, in assertContains
text_repr, real_count, msg_prefix, content_repr = self._assert_contains(
^^^^^^^^^^^^^^^^^^^^^^
File "/Users/mariacam/.pyenv/versions/3.12.5/lib/python3.12/site-packages/django/test/testcases.py", line 571, in _assert_contains
self.assertEqual(
AssertionError: 302 != 200 : Couldn't retrieve content: Response code was 302 (expected 200)
======================================================================
FAIL: test_new_topic_valid_post_data (boards.tests.test_new_topic_tests.NewTopicTests.test_new_topic_valid_post_data)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/Users/mariacam/Python-Development/django-boards/django_boards/boards/tests/test_new_topic_tests.py", line 30, in test_new_topic_valid_post_data
self.assertTrue(Topic.objects.exists())
AssertionError: False is not true
----------------------------------------------------------------------
Ran 4 tests in 0.864s
FAILED (failures=2)
Destroying test database for alias 'default'...
I am new to Django tests. Any feedback would be very appreciated. Thanks!