Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test cases failing with 404 error #15

Open
ghost opened this issue Jul 11, 2018 · 1 comment
Open

Test cases failing with 404 error #15

ghost opened this issue Jul 11, 2018 · 1 comment

Comments

@ghost
Copy link

ghost commented Jul 11, 2018

Hello!

I am following this guide and stumbled on an weird issue. The website works perfectly when i try it in the web browser. But it seems that all the test cases are failing with 404 error page not found (even though i can access everything myself via web browser).
OS: Debian GNU/Linux 9.4 (stretch), Python: Python 3.5.3, Django: (2, 0, 7, 'final', 0).
urls.py
#######
from django.contrib import admin
from django.urls import path, re_path
from boards import views as board_views
from django.conf.urls import url

urlpatterns = [
path('', board_views.home, name='home'),
path('boards/int:pk/', board_views.board_topics, name='board_topics'),
path('boards/int:pk/new/', board_views.new_topic, name='new_topic'),
path('admin/', admin.site.urls),
]
#######

#######
views.py:
from django.shortcuts import render, redirect, get_object_or_404
from django.http import HttpResponse
from .models import Board, Topic, Post

def board_topics(request, pk):
board = get_object_or_404(Board, pk=pk)
return render(request, 'topics.html', {'board': board})

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

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

When i print the variables, i can see that DB object exists, but 404 page not found is returned:
<QuerySet [<Board: Django>]>
b'

Not Found

The requested URL /boards/1/ was not found on this server.

'
404

Thanks!

@mapsic
Copy link

mapsic commented Apr 26, 2019

Maybe you can try the follow python code.

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

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant