Skip to content

Basic 101

Debug with a Simplified Test

Create a minimal test case to isolate the issue.

# custom_apps/accounts/tests/test_basic.py
from django.test import TestCase

class BasicTests(TestCase):
    def test_basic(self):
        self.assertTrue(True)

Run this test:

python3 manage.py test custom_apps.accounts.tests.test_basic

Verify Import Paths

When running tests, Django uses the module path relative to your Django project’s root directory. Ensure that you are using the correct path to the test module.

Try running the test using the full module path:

python3 manage.py test custom_apps.accounts.tests.test_authentication

Check for __init__.py Files

Ensure that each directory in your app has an __init__.py file, including the tests directory. This file can be empty but is necessary for Python to recognize the directory as a package.

accounts/tests/__init__.py:

# This file can be empty

Test Django’s Shell Import

Try importing the test_basic module manually from Django’s shell to verify that it can be loaded correctly.

python3 manage.py shell

>>> from custom_apps.accounts.tests.test_basic import BasicTests
test_basic.py
# custom_apps/accounts/tests/test_basic.py
from django.test import TestCase


class BasicTests(TestCase):
    def test_basic(self):
        self.assertTrue(True)

If the import fails, there might be an issue with the module path or the file itself.

Summary

  • Verify Directory Structure: Ensure the accounts app and tests directory are correctly structured.
  • Check INSTALLED_APPS: Confirm custom_apps.accounts is in INSTALLED_APPS.
  • Use Correct Path: Run tests using the correct full module path.
  • Ensure __init__.py Exists: Check that all directories have __init__.py files.
  • Debug Imports: Use Django’s shell to test imports manually.
  • Simplify and Test: Create a minimal test case to isolate issues.

By following these steps, you should be able to identify and resolve the issue preventing your test module from being imported and executed.