Module Not Found Error when trying to use Test functionality - solved

Solved - It was a simple naming convention error after all - i had failed to start the def within the test python file with `test_’. I had mistakenly only associated that with the files not the function names - many thanks to Suttonium

Hi,

I am new to Django, I have gone through the tutorial, more using it as a template to build an early version of the site I am working on. It has worked fine, and I have got my site up and running ok using the inbuilt server, however what has not worked is using the test functionality.

I have read the other threads on here, and am really not sure what it could be. I see there have been another thread or two where people started again and then got round their problems, but I have been through everything I can think of. I am wondering if I am missing some naming convention … who knows … hopefully you :slight_smile:

I have a project folder with a single app in it and i initially put a test file into the app root director. This just resulted in 0 tests being run, no matter what arguments i gave the command line (including/excluding app) etc.

I then read up a bit and whilst that should of worked, I moved my test file to a folder tests under the app folder and added an init.py file to the test folder before adding a line to import all from the tests.py file.

I now get the following error:

     % python3.8 manage.py test appname -v3
    Skipping setup of unused database(s): default.
    System check identified no issues (0 silenced).
    appname.tests (unittest.loader._FailedTest) ... ERROR

    ======================================================================
    ERROR: appname.tests (unittest.loader._FailedTest)
    ----------------------------------------------------------------------
    ImportError: Failed to import test module: appname.tests
    Traceback (most recent call last):
      File "/usr/local/Cellar/python@3.8/3.8.5/Frameworks/Python.framework/Versions/3.8/lib/python3.8/unittest/loader.py", line 470, in _find_test_path
        package = self._get_module_from_name(name)
      File "/usr/local/Cellar/python@3.8/3.8.5/Frameworks/Python.framework/Versions/3.8/lib/python3.8/unittest/loader.py", line 377, in _get_module_from_name
        __import__(name)
      File "/Users/username/git/django/sitename/appname/tests/__init__.py", line 1, in <module>
        from tests.py import *
    ModuleNotFoundError: No module named 'tests'

    ----------------------------------------------------------------------
    Ran 1 test in 0.000s

    FAILED (errors=1)

The test file looks like this:

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

    class testmy(TestCase):

        def index_page_loads_ok(self):
            print("hello")

I removed everything I could to make it as simple as possible.

The init.py within the appname/tests folder looks like this

         from tests.py import *

I am running on Mac using Python 3.85.

My current folder structure below the project folder looks like this:

β”œβ”€β”€ db.sqlite3
β”œβ”€β”€ manage.py
β”œβ”€β”€ appname
β”‚   β”œβ”€β”€ __init__.py
β”‚   β”œβ”€β”€ admin.py
β”‚   β”œβ”€β”€ apps.py
β”‚   β”œβ”€β”€ migrations
β”‚   β”‚   β”œβ”€β”€ 0001_initial.py
β”‚   β”‚   β”œβ”€β”€ __init__.py
β”‚   β”œβ”€β”€ models.py
β”‚   β”œβ”€β”€ static
β”‚   β”‚   └── appname
β”‚   β”‚       β”œβ”€β”€ graphics
β”‚   β”‚       β”‚   β”œβ”€β”€ addsymbolround.png
β”‚   β”‚       β”‚   β”œβ”€β”€ documentation-photo.jpg
β”‚   β”‚       β”‚   └── background.jpeg
β”‚   β”‚       └── styles.css
β”‚   β”œβ”€β”€ templates
β”‚   β”‚   └── appname
β”‚   β”‚       β”œβ”€β”€ entities.html
β”‚   β”‚       └── index.html
β”‚   β”œβ”€β”€ tests
β”‚   β”‚   β”œβ”€β”€ __init__.py
β”‚   β”‚   └── tests.py
β”‚   β”œβ”€β”€ urls.py
β”‚   └── views.py
└── sitename
    β”œβ”€β”€ asgi.py
    β”œβ”€β”€ settings.py
    β”œβ”€β”€ urls.py
    └── wsgi.py

It feels like it is a simple mistake I have made but I just can’t figure it out so any help would be appreciated.

I don’t think you need any code in the __init__.py file. Whenever I break my tests into multiple files like this I have never had to add an import statement for the tests to be recognized.

Also, if you are going to import, you shouldn’t need to add .py in the import statement. It should be from tests import *.

Hi, thanks for that, I tried both suggestions, i removed the whole line from init which means i now get:


System check identified no issues (0 silenced).

----------------------------------------------------------------------
Ran 0 tests in 0.000s

OK

so back to not finding anything.

Then i put the import back (i got it off another thread somewhere) and just remove the .py i got back to the previous error:

System check identified no issues (0 silenced).
E
======================================================================
ERROR: appname.tests (unittest.loader._FailedTest)
----------------------------------------------------------------------
ImportError: Failed to import test module: appname.tests
Traceback (most recent call last):
  File "/usr/local/Cellar/python@3.8/3.8.5/Frameworks/Python.framework/Versions/3.8/lib/python3.8/unittest/loader.py", line 470, in _find_test_path
    package = self._get_module_from_name(name)
  File "/usr/local/Cellar/python@3.8/3.8.5/Frameworks/Python.framework/Versions/3.8/lib/python3.8/unittest/loader.py", line 377, in _get_module_from_name
    __import__(name)
  File "/Users/username/git/django/projectname/appname/tests/__init__.py", line 1, in <module>
    from tests import *
ModuleNotFoundError: No module named 'tests'


----------------------------------------------------------------------
Ran 1 test in 0.000s

FAILED (errors=1)

it just doesn’t seem to be picking up test files, or if its ignoring them because of errors it doesn’t tell me why even in -v3.

The first output you pasted is correct behavior because your function inside your test class does not start with β€˜test’. You’d want it to be this:

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

    class testmy(TestCase):

        def test_index_page_loads_ok(self):
            print("hello")

Notice the added word β€˜test’ to the function definition :slight_smile:

1 Like

Thank you, that’s done it.

Hi! I have got the same problem, I have been advised to delete init.py file, I did it anr that’s walked.Therefore I have one question, what will be the consequences in the future ?
Thanks!

You shouldn’t delete the __init__.py file because that’s how Django recognizes your testing module(s). Do you have any code you want to add to this thread?

But the problem is that, if I restaure this file, django will not be able to lunch unit test anymore. Unless there is some peace of code I can use in order to make work. Thanks!

Would you mind posting the code you’re referring to (test files mainly)?

I’m on my way home. As soon as I arrive I just post it. Thanks in advance!!

PS C:\Users\musfi\Desktop\project-3-main\project> python manage.py test -v2
Found 0 test(s).
Skipping setup of unused database(s): default.
System check identified no issues (0 silenced).


Ran 0 tests in 0.000s

NO TESTS RAN
PS C:\Users\musfi\Desktop\project-3-main\project> Hello can you help me ? when I write the code I see this result. Ran 0 tests but there are many tests. do you know how to solve ?