216
I had exactly the same issue with my Django project:
$ python manage test polls.tests
worked fine whereas the following failed with an import error:
$ python manage test polls
$ python manage test
(...)
ImportError: Failed to import test module: mydjango.polls.tests
Traceback (most recent call last):
(...)
ImportError: No module named polls.tests
Check carefully the error message: Djangoβs test runner tries to import the tests from mydjango.polls.tests where mydjango is the name of the root directory (the container for your project).
I fixed this issue by deleting the __init__.py
file in mydjango directory (at the same level than manage.py file). This directory is not supposed to be a python module and it seems to mess up with Djangoβs test runner if it is the case.
So just deleting the _init_.py file should fix our problem:
$ rm mydjango/__init__.py
20
For anyone else having the same problem, another reason for this to happen is if you have the same name for the root folder and the project folder.
For example:
mydjango
βββ __init__.py
βββ manage.py
βββ mydjango
β βββ __init__.py
β βββ settings.py
β βββ urls.py
β βββ wsgi.py
βββ polls
β βββ admin.py
β βββ __init__.py
β βββ models.py
| βββ tests.py
β βββ templates
running
./manage.py test
throws errors No module named polls.tests
to fix it simply rename the root folder to something else like :
mydjango_project
βββ __init__.py
βββ manage.py
βββ mydjango
β βββ __init__.py
β βββ settings.py
β βββ urls.py
β βββ wsgi.py
βββ polls
β βββ admin.py
β βββ __init__.py
β βββ models.py
| βββ tests.py
β βββ templates
- [Django]-Django Deprecation Warning or ImproperlyConfigured error β Passing a 3-tuple to django.conf.urls.include() is not supported
- [Django]-Django modifying the request object
- [Django]-In a django model custom save() method, how should you identify a new object?
2
Anyhow running
$ python manage.py test polls.tests
It works, itβs enough for me right now:
Creating test database for alias 'default'...
F
======================================================================
FAIL: test_was_published_recently_with_future_poll (polls.tests.QuestionMethodTests)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/sergio/.virtualenvs/django4/mydjango/polls/tests.py", line 17, in test_was_published_recently_with_future_poll
self.assertEqual(future_question.was_published_recently(), False)
AssertionError: True != False
- [Django]-Uninstall Django completely
- [Django]-How to get the current language in Django?
- [Django]-Cannot apply DjangoModelPermissions on a view that does not have `.queryset` property or overrides the `.get_queryset()` method
0
first answer didnβt work for me. im using win8, may be this is a reason.
in terminal try to change dir to ./polls and the run
python ../manage.py test polls
- [Django]-Django using get_user_model vs settings.AUTH_USER_MODEL
- [Django]-How to make two django projects share the same database
- [Django]-Constructing Django filter queries dynamically with args and kwargs