Django tests doesnt work

When i run the command “python manage.py test” I am getting the list of errors like this (beloved)

I can run a simple test in my application (shop) with the command “python manage.py test shop”

from django.test import SimpleTestCase

class TestSimpleCase(SimpleTestCase):

    def test1(self):
        assert 1 == 2

    def test2(self):
        assert 2 == 2
assert 1 == 2
       ^^^^^^

AssertionError

But when I am trying to create test that relates to the application like:

from django.test import SimpleTestCase
from django.urls import resolve, reverse
from shop.views import product_list


class TestUrls(SimpleTestCase):

    def test1(self):
        url = reverse('product_list')
        print(resolve(url))

path(‘’, views.product_list, name=‘product_list’),

I am getting this error:

raise NoReverseMatch(msg)

django.urls.exceptions.NoReverseMatch: Reverse for ‘product_list’ not found. ‘product_list’ is not a valid view function or pattern name.


Ran 1 test in 0.007s

LIST OF ERROR AFTER RUNNING “python manage.py test”

Found 9 test(s).
System check identified no issues (0 silenced).
EEEEEEEEE

ERROR: myshop.accounts (unittest.loader._FailedTest.myshop.accounts)

ImportError: Failed to import test module: myshop.accounts
Traceback (most recent call last):
File “C:\Users\Kacper\AppData\Local\Programs\Python\Python311\Lib\unittest\loader.py”, line 440, in _find_test_path
package = self._get_module_from_name(name)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “C:\Users\Kacper\AppData\Local\Programs\Python\Python311\Lib\unittest\loader.py”, line 350, in _get_module_from_name
import(name)
ModuleNotFoundError: No module named ‘myshop.accounts’

======================================================================
ERROR: myshop.api (unittest.loader._FailedTest.myshop.api)

ImportError: Failed to import test module: myshop.api
Traceback (most recent call last):
File “C:\Users\Kacper\AppData\Local\Programs\Python\Python311\Lib\unittest\loader.py”, line 440, in _find_test_path
package = self._get_module_from_name(name)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “C:\Users\Kacper\AppData\Local\Programs\Python\Python311\Lib\unittest\loader.py”, line 350, in _get_module_from_name
import(name)
ModuleNotFoundError: No module named ‘myshop.api’

======================================================================
ERROR: myshop.cart (unittest.loader._FailedTest.myshop.cart)

ImportError: Failed to import test module: myshop.cart
Traceback (most recent call last):
File “C:\Users\Kacper\AppData\Local\Programs\Python\Python311\Lib\unittest\loader.py”, line 440, in _find_test_path
package = self._get_module_from_name(name)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “C:\Users\Kacper\AppData\Local\Programs\Python\Python311\Lib\unittest\loader.py”, line 350, in _get_module_from_name
import(name)
ModuleNotFoundError: No module named ‘myshop.cart’

======================================================================
ERROR: myshop.coupons (unittest.loader._FailedTest.myshop.coupons)

ImportError: Failed to import test module: myshop.coupons
Traceback (most recent call last):
File “C:\Users\Kacper\AppData\Local\Programs\Python\Python311\Lib\unittest\loader.py”, line 440, in _find_test_path
package = self._get_module_from_name(name)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “C:\Users\Kacper\AppData\Local\Programs\Python\Python311\Lib\unittest\loader.py”, line 350, in _get_module_from_name
import(name)
ModuleNotFoundError: No module named ‘myshop.coupons’

======================================================================
ERROR: myshop.myshop (unittest.loader._FailedTest.myshop.myshop)

ImportError: Failed to import test module: myshop.myshop
Traceback (most recent call last):
File “C:\Users\Kacper\AppData\Local\Programs\Python\Python311\Lib\unittest\loader.py”, line 440, in _find_test_path
package = self._get_module_from_name(name)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “C:\Users\Kacper\AppData\Local\Programs\Python\Python311\Lib\unittest\loader.py”, line 350, in _get_module_from_name
import(name)
ModuleNotFoundError: No module named ‘myshop.myshop’

======================================================================
ERROR: myshop.order (unittest.loader._FailedTest.myshop.order)

ImportError: Failed to import test module: myshop.order
Traceback (most recent call last):
File “C:\Users\Kacper\AppData\Local\Programs\Python\Python311\Lib\unittest\loader.py”, line 440, in _find_test_path
package = self._get_module_from_name(name)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “C:\Users\Kacper\AppData\Local\Programs\Python\Python311\Lib\unittest\loader.py”, line 350, in _get_module_from_name
import(name)
ModuleNotFoundError: No module named ‘myshop.order’

======================================================================
ERROR: myshop.payment (unittest.loader._FailedTest.myshop.payment)

ImportError: Failed to import test module: myshop.payment
Traceback (most recent call last):
File “C:\Users\Kacper\AppData\Local\Programs\Python\Python311\Lib\unittest\loader.py”, line 440, in _find_test_path
package = self._get_module_from_name(name)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “C:\Users\Kacper\AppData\Local\Programs\Python\Python311\Lib\unittest\loader.py”, line 350, in _get_module_from_name
import(name)
ModuleNotFoundError: No module named ‘myshop.payment’

======================================================================
ERROR: myshop.shop_info (unittest.loader._FailedTest.myshop.shop_info)

ImportError: Failed to import test module: myshop.shop_info
Traceback (most recent call last):
File “C:\Users\Kacper\AppData\Local\Programs\Python\Python311\Lib\unittest\loader.py”, line 440, in _find_test_path
package = self._get_module_from_name(name)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “C:\Users\Kacper\AppData\Local\Programs\Python\Python311\Lib\unittest\loader.py”, line 350, in _get_module_from_name
import(name)
ModuleNotFoundError: No module named ‘myshop.shop_info’

======================================================================
ERROR: myshop.shopcontact (unittest.loader._FailedTest.myshop.shopcontact)

ImportError: Failed to import test module: myshop.shopcontact
Traceback (most recent call last):
File “C:\Users\Kacper\AppData\Local\Programs\Python\Python311\Lib\unittest\loader.py”, line 440, in _find_test_path
package = self._get_module_from_name(name)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “C:\Users\Kacper\AppData\Local\Programs\Python\Python311\Lib\unittest\loader.py”, line 350, in _get_module_from_name
import(name)
ModuleNotFoundError: No module named ‘myshop.shopcontact’


Ran 9 tests in 0.001s

FAILED (errors=9)

FIle structure

What does your urls.py file look like? (Do you have a url named product_list?)

What is your current directory when you’re running this?

This is how my urls.py looks like

from django.urls import path
from . import views

app_name = ‘shop’

urlpatterns = [

path('', views.product_list, name='product_list'),

path('search/', views.search, name='search'),

path('<str:category_slug>/', views.product_list, name='product_list_by_category'),

path('<int:id>/<slug:slug>/', views.product_detail, name='product_detail'),

path('<int:id>/<slug:slug>/rate', views.product_rate, name='product_rate'),

]

and when I am running command python manage.py test shop I and in >myshop which is the main directory where manage.py is located

Side note: When pasting code here, surround the code between lines of three backtick - ` characters. This means you’ll have a line of ```, then your code (or template, or traceback or whatever), then another line of ```. This forces the forum software to keep your code (template, etc) properly formatted.

This creates a namespace for your urls. You need to use the namespace name when trying to reference the urls within that namespace. (e.g. shop:product_list)

1 Like

I tried this but still have the same error

path('', views.product_list, name='shop:product_list'),

raise NoReverseMatch(msg)
django.urls.exceptions.NoReverseMatch: Reverse for ‘product_list’ not found. ‘product_list’ is not a valid view function or pattern name.


Ran 1 test in 0.007s

And after running the command python manage.py test I still getting the whole list of errors, for example:

import(name)
ModuleNotFoundError: No module named ‘myshop.shopcontact’

But the project works fine

It’s not the url name that needs to be changed, it’s the reference to the name. (The url that you’re trying to reverse.)

Okey now it works
Thank you so much!