0

I'm trying to do simple tests in Django (v. 2.0.5). Since I can not see why I'm getting the '404 != 200' error, I post all relevant data.

test.py

from django.urls import resolve, reverse
from django.test import TestCase
from .views import home, board_topics
from .models import Board


class HomeTests(TestCase):
    def test_home_view_status_code(self):
        url = reverse('home')
        response = self.client.get(url)
        self.assertEqual(response.status_code, 200)

    def test_home_url_resolves_home_view(self):
        view = resolve('/home/')
        self.assertEqual(view.func, home)


class BoardTopicsTests(TestCase):
    def setUp(self):
        Board.objects.create(name='Django', description='Django discussion board')

    def test_board_topics_view_success_status_code(self):
        url = reverse('board_topics', kwargs={'pk': 1})
        response = self.client.get(url)
        self.assertEqual(response.status_code, 200)

    def test_board_topics_view_not_found_status_code(self):
        url = reverse('board_topics', kwargs={'pk': 99})
        response = self.client.get(url)
        self.assertEqual(response.status_code, 404)

    def test_board_topics_url_resolves_board_topics_view(self):
        view = resolve('/boards/1/')
        self.assertEqual(view.func, board_topics)

urls.py

from django.contrib import admin
from django.urls import include, path
from boards import views

urlpatterns = [
    path('boards/<int:pk>/', views.board_topics, name='board_topics'),
    path('home/', views.home, name='home'),
    path('admin/', admin.site.urls),
]

views.py

...
def board_topics(request, pk):
    try:
        board = Board.objects.get(pk=pk)
    except Board.DoesNotExist:
        raise Http404
    return render(request, 'topics.html', {'board': board})

Traceback

FAIL: test_board_topics_view_success_status_code (boards.tests.BoardTopicsTests)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/.../boards/tests.py", line 25, in test_board_topics_view_success_status_code
self.assertEqual(response.status_code, 200)
    AssertionError: 404 != 200

I wonder why I'm getting this error because I can call the views and I also get a 404 error when I try to call a page that does not exist (except Board.DoesNotExist). Is there a way to make the tests different (easier)? Thanks in advance for help.

1
  • Board.objects.get(pk=1) leads to Board.DoesNotExist - did you create some fixtures/test data in the DB specified for tests? Commented May 29, 2018 at 19:02

2 Answers 2

1

You could try to adjust your test to automatically pickup the ID of the created object.

class BoardTopicsTests(TestCase):
    def setUp(self):
        self.board = Board.objects.create(name='Django', description='Django discussion board')

    def test_board_topics_view_success_status_code(self):
        url = reverse('board_topics', kwargs={'pk': self.board.id})

Maybe the database is not properly cleared between the tests?

Sign up to request clarification or add additional context in comments.

Comments

0

Use resolve('/') instead of resolve('/home/')

def test_home_url_resolves_home_view(self):
    view = resolve('/')
    self.assertEquals(view.func, home)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.