[python] AttributeError : ‘모듈’개체에 ‘테스트’속성이 없습니다.

이 명령을 실행하고 있습니다.

python manage.py test project.apps.app1.tests

이 오류가 발생합니다.

AttributeError : ‘모듈’개체에 ‘테스트’속성이 없습니다.

아래는 내 디렉토리 구조입니다. 또한 설치된 앱 구성에 app1을 추가했습니다.

Traceback (most recent call last):
    File "manage.py", line 10, in <module> execute_from_command_line(sys.argv)
    File "/home/username/local/dev/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 385, in execute_from_command_line
    utility.execute()
    File "/home/username/local/dev/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 377, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
    File "/home/username/local/dev/local/lib/python2.7/site-packages/django/core/management/commands/test.py", line 50, in run_from_argv
    super(Command, self).run_from_argv(argv)
    File "/home/username/local/dev/local/lib/python2.7/site-packages/django/core/management/base.py", line 288, in run_from_argv
    self.execute(*args, **options.__dict__)
    File "/home/username/local/dev/local/lib/python2.7/site-packages/django/core/management/commands/test.py", line 71, in execute
    super(Command, self).execute(*args, **options)
    File "/home/username/local/dev/local/lib/python2.7/site-packages/django/core/management/base.py", line 338, in execute
    output = self.handle(*args, **options)
    File "/home/username/local/dev/local/lib/python2.7/site-packages/django/core/management/commands/test.py", line 88, in handle
    failures = test_runner.run_tests(test_labels)
    File "/home/username/local/dev/local/lib/python2.7/site-packages/django/test/runner.py", line 146, in run_tests
    suite = self.build_suite(test_labels, extra_tests)
    File "/home/username/local/dev/local/lib/python2.7/site-packages/django/test/runner.py", line 66, in build_suite
    tests = self.test_loader.loadTestsFromName(label)
    File "/usr/lib/python2.7/unittest/loader.py", line 100, in loadTestsFromName
    parent, obj = obj, getattr(obj, part)
    AttributeError: 'module' object has no attribute 'tests'

디렉토리 구조 :

여기에 이미지 설명 입력



답변

나는 마침내 다른 문제를 해결하기 위해 그것을 알아 냈습니다. 문제는 내 테스트에서 가져 오기를 찾을 수 없다는 것입니다.

테스트를 가져 오지 못하면 위의 오류가 발생하는 것 같습니다. 이는 테스트 스위트가 손상된 테스트를 가져올 수 없기 때문에 의미가 있습니다. 적어도 내 테스트 파일 내에서 가져 오기를 수정하고 제대로 작동하기 시작했는지 확인했기 때문에 이것이 진행되고 있다고 생각합니다.

테스트 케이스의 유효성을 검사하려면 Python 콘솔에서 테스트 케이스 파일을 가져 오십시오.

예:

from project.apps.app1.tests import *


답변

사용하다:

./manage.py shell

뒤에

import myapp.tests

가져 오기 오류의 특성을 찾으려면


답변

내 경우를 들어, 나는 만들 필요가 빈 __init__.py을 내에서 app/tests폴더


답변

위의 Steve Bradshaw의 예는 가져 오기 오류에 대해 작동합니다 (Steve에게 감사합니다).

다른 유형의 오류 (예 : ValueError)도 발생할 수 있습니다.

AttributeError: 'module' object has no attribute 'tests'

이 오류가 무엇인지 확인하려면

./manage.py shell
from myapp.tests import SomeTestCase
t = SomeTestCase()


답변

Chris와 같은 오류가 발생했습니다. 이전 모델을 삭제 한 다음 tests.py를 실행했지만 다른 파일 (views.py)이 여전히 삭제 된 모델을 가져 오려고했습니다.

지금은 사용되지 않는 import 문을 꺼냈을 때 문제가 해결되었습니다.


답변

스크립트에서 사용중인 모든 모듈이 손상되지 않았는지 확인하십시오. 이것은 수입 명세서에서 철자를 검사하는 것을 의미합니다.

# invalid import
from app.model.notification import Notification
# valid import
from app.models.notification import Notification

djano의 대화 형 콘솔에서 imports 문을 실행하여 모듈을 테스트 할 수 있습니다.

$root@13faefes8: python manage.py shell
Type "help", "copyright", "credits" or "license" for more information (InteractiveConsole)
>>> from app.model.notification import Notification
Traceback (most recent call last):
   File "<console>", line 1, in <module>
ImportError: No module named model.notification


답변

순환 가져 오기 참조를 수정하여 “AttributeError : module ‘utils’has no attribute ‘name_of_my_function'”오류를 해결했습니다. 내 파일 manage.py와 utils.py에는 각각 서로를 가리키는 import 문이 있습니다.