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:
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:
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
:
Test Django’s Shell Import
Try importing the test_basic
module manually from Django’s shell to verify that it can be loaded correctly.
# 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 andtests
directory are correctly structured. - Check
INSTALLED_APPS
: Confirm custom_apps.accounts is inINSTALLED_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.