mardi 13 avril 2021

Django, unit test not working: reverse for xxx is not found. xxx is not a valid view function or pattern name

I understand there are a few questions like this on stack overflow, but none of them seem to answer my issue. I am trying to run a unit test on my pages in the project, but I keep getting this issue: "Reverse for 'page1' not found. 'page1' is not a valid view function or pattern name."

'page1' works fine when I run it with python manage.py runserver, but when I run python manage.py test it fails.

urls.py

from django.urls import path
from . import views

urlpatterns = [
    path('', views.displaylol),
    path('page1', views.page1),
    path('empty', views.empty),
]

views.py

from django.shortcuts import render
from django.http import HttpResponse
from .models import Tag

# Create your views here.

def displaylol(request):
    return HttpResponse("Home page")

def page1(request):
    tags = Tag.objects.all()
    return render(request, 'ecommerce/page1.html', {'tags': tags})

def empty(request):
    return HttpResponse("empty")

ecommerce/page1.html

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Product 1</title>
    </head>
    <body>
        <h1>Tags</h1>

        <table border="1">
            <thead>
                <tr>
                    <th>Tag</th>
                    <th>Connections</th>
                    <th>No. of products attached to</th>
                    <th>Idk</th>
                </tr>
            </thead>
            <tbody>
                
            </tbody>
        </table>
    </body>
</html>

tests.py

from django.urls import reverse
from django.test import TestCase

# Create your tests here.

class page1Tests(TestCase):
    def test_home_view_status_code(self):
        url = reverse('page1') # It fails the same way when I use reverse('empty') as well
        response = self.client.get(url)
        self.assertEquals(response.status_code, 200)

the error:

(venv) xxx@xxx-MacBook 03_high_level % python manage.py test
Creating test database for alias 'default'...
System check identified no issues (0 silenced).
E
======================================================================
ERROR: test_home_view_status_code (ecommerce.tests.page1Tests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Users/xxx/Documents/OneDrive/#Development#/PROJ_NEA_03/03_high_level/ecommerce/tests.py", line 9, in test_home_view_status_code
    url = reverse('page1')
  File "/Users/xxx/Documents/OneDrive/#Development#/PROJ_NEA_03/venv/lib/python3.8/site-packages/django/urls/base.py", line 87, in reverse
    return iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs))
  File "/Users/xxx/Documents/OneDrive/#Development#/PROJ_NEA_03/venv/lib/python3.8/site-packages/django/urls/resolvers.py", line 685, in _reverse_with_prefix
    raise NoReverseMatch(msg)
django.urls.exceptions.NoReverseMatch: Reverse for 'page1' not found. 'page1' is not a valid view function or pattern name.

----------------------------------------------------------------------
Ran 1 test in 0.008s

FAILED (errors=1)
Destroying test database for alias 'default'...

Aucun commentaire:

Enregistrer un commentaire