Running django tutorial tests fail - No module named polls.tests
Asked Answered
F

4

64

I'm playing with django 1.6 tutorial but i can't run tests. My project (name mydjango) and app structure (name is polls) are as shown below in a virtualenv. (.nja files are just created by ninja-ide the ide I'm using)

.
├── __init__.py
├── manage.py
├── mydjango
│   ├── __init__.py
│   ├── __init__.pyc
│   ├── mydjango.nja
│   ├── settings.py
│   ├── settings.pyc
│   ├── templates
│   │   └── admin
│   │       └── base_site.html
│   ├── urls.py
│   ├── urls.pyc
│   ├── wsgi.py
│   └── wsgi.pyc
├── polls
│   ├── admin.py
│   ├── admin.pyc
│   ├── __init__.py
│   ├── __init__.pyc
│   ├── models.py
│   ├── models.pyc
│   ├── templates
│   │   ├── __init__.py
│   │   └── polls
│   │       ├── detail.html
│   │       ├── index.html
│   │       ├── __init__.py
│   │       └── results.html
│   ├── tests.py
│   ├── tests.pyc
│   ├── urls.py
│   ├── urls.pyc
│   ├── views.py
│   └── views.pyc
└── polls.nja

I followed the tutorial to understand how django works but I'm stuck in the test part. As tutorial suggest I created a file named tests.py into the app folder, the pretty straightforward file is:

# -*- coding: utf-8 -*-
from django.test import TestCase
import datetime
from django.utils import timezone
from polls.models import Question

# Create your tests here.l  
class QuestionMethodTests(TestCase):

    def test_was_published_recently_with_future_poll(self):
        """
        was_published_recently dovrebbe ritornare falso se si mette una data nel futuro
        """
        future_question = Question(pub_date=timezone.now() + datetime.timedelta(hours=50))
        self.assertEqual(future_question.was_published_recently(), False)

then i installed unittest2 into the virtualenv with

$pip install unittest2

and run

$python manage.py test polls
Creating test database for alias 'default'...
E
======================================================================
ERROR: mydjango.polls.tests (unittest2.loader.ModuleImportFailure)
----------------------------------------------------------------------
ImportError: Failed to import test module: mydjango.polls.tests
Traceback (most recent call last):
  File "/home/sergio/.virtualenvs/django4/local/lib/python2.7/site-packages/unittest2/loader.py", line 260, in _find_tests
    module = self._get_module_from_name(name)
  File "/home/sergio/.virtualenvs/django4/local/lib/python2.7/site-packages/unittest2/loader.py", line 238, in _get_module_from_name
    __import__(name)
ImportError: No module named polls.tests


----------------------------------------------------------------------
Ran 1 test in 0.001s

FAILED (errors=1)
Destroying test database for alias 'default'...

No way to have the test working, also if don't pass the app name it returns the same error:

$ python manage.py test
Creating test database for alias 'default'...
E
======================================================================
ERROR: mydjango.polls.tests (unittest2.loader.ModuleImportFailure)
----------------------------------------------------------------------
ImportError: Failed to import test module: mydjango.polls.tests
Traceback (most recent call last):
  File "/home/sergio/.virtualenvs/django4/local/lib/python2.7/site-packages/unittest2/loader.py", line 260, in _find_tests
    module = self._get_module_from_name(name)
  File "/home/sergio/.virtualenvs/django4/local/lib/python2.7/site-packages/unittest2/loader.py", line 238, in _get_module_from_name
    __import__(name)
ImportError: No module named polls.tests


----------------------------------------------------------------------
Ran 1 test in 0.001s

FAILED (errors=1)
Destroying test database for alias 'default'...

My INSTALLED_APPS are:

INSTALLED_APPS = (
    'south',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'polls',
)

What am I doing wrong?

Floccose answered 12/1, 2014 at 0:44 Comment(4)
syncdb and runserver commands work?Fasciate
yes both of them, i mean they run without errorsFloccose
What OS did you have when encountered problem? We can't reproduce it's on Ubuntu, though have it on MacAffairs
@Affairs the os was ubuntu server 14.04 but as you can see it was some time ago - maybe things changedFloccose
F
221

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
Fissure answered 28/2, 2014 at 13:33 Comment(8)
Also if you're following the Django 1.9 tutorial to a tee, there's a typo at the command to run your new unit test. Make sure to run python manage.py test polls.tests not the documented python manage.py test pollsUmbles
@DylanPierce I'm pretty sure that the docs are correct. Running python manage.py test polls works for me, as long as I don't have an __init__.py in my project folder.Gonnella
Lifesaver.. Thanks "So just deleting the init.py file should fix our problem: " worked like a charm.. :)Elysia
Good spot, however the Django docs (section 2) state that we should use __init__.py to define models (I've seen migration break if this is not done). So this could be a Django bug.Jacklynjackman
@MatthewHegarty this is not the same __init__.py file: Django documentation mentions models/__init__.py, the file to delete here is the project's __init__.py file (this file should not exist).Fissure
Thanks a lot. I got this error after upgrading from django 2.7 to django 3. Resolving the init.py file solved the problem.Litchfield
Deleting the init.py file located at the project's root folderr solved the issue for me. Very good to know, had been stuck for a while because of that.Evora
Removing the __init__.py file solved my problem. Thanks.Knickknack
M
21

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
Melodramatize answered 12/2, 2015 at 11:32 Comment(1)
Alternatively, in Django 1.4+ you should remove mydjango/__init__.py, check github.com/django-nose/django-nose/issues/81Ascogonium
F
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
Floccose answered 13/1, 2014 at 23:15 Comment(2)
While this is a viable work-around, I believe that pchiquet's answer should be selected as the correct answer as it actually solves the problem.Gateway
+1 the accepted question should be the @Fissure one.Howdy
E
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
Emmet answered 24/2, 2014 at 4:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.