code

django 튜토리얼 테스트 실행 실패-polls.tests라는 모듈이 없습니다.

codestyles 2021. 1. 9. 09:54
반응형

django 튜토리얼 테스트 실행 실패-polls.tests라는 모듈이 없습니다.


django 1.6 튜토리얼을 가지고 놀고 있지만 테스트를 실행할 수 없습니다. 내 프로젝트 (이름 mydjango)와 앱 구조 (이름은 polls)는 virtualenv에서 아래와 같습니다. (.nja 파일은 ninja-ide 내가 사용하는 ide에 의해 생성되었습니다)

.
├── __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

django가 어떻게 작동하는지 이해하기 위해 튜토리얼을 따랐지만 테스트 부분에 갇혀 있습니다. 튜토리얼에 따르면 앱 폴더에 tests.py라는 파일을 만들었을 때 매우 간단한 파일은 다음과 같습니다.

# -*- 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)

그런 다음 unittest2를 virtualenv에 설치했습니다.

$pip install unittest2

그리고 실행

$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'...

테스트를 작동시킬 방법이 없으며 앱 이름을 전달하지 않으면 동일한 오류가 반환됩니다.

$ 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'...

내 INSTALLED_APPS는 다음과 같습니다.

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

내가 도대체 ​​뭘 잘못하고있는 겁니까?


내 Django 프로젝트에서 똑같은 문제가 발생했습니다.

$ python manage test polls.tests

다음은 가져 오기 오류로 실패했지만 잘 작동했습니다.

$ 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

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

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

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

ReferenceURL : https://stackoverflow.com/questions/21069880/running-django-tutorial-tests-fail-no-module-named-polls-tests

반응형