AttributeError:“模块”对象没有属性“测试”

问题: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'

目录结构:

I’m running this command:

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

and it causes this error:

AttributeError: ‘module’ object has no attribute ‘tests’

Below is my directory structure. I’ve also added app1 to my installed apps config.

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'

Directory structure:


回答 0

我终于想出了解决另一个问题的方法。问题是我的测试找不到导入。

如果您的测试无法导入,您似乎会收到上述错误。这是有道理的,因为测试套件无法导入损坏的测试。至少我认为这是正在发生的事情,因为我在测试文件中修复了导入,并确定导入已开始起作用。

要验证您的测试用例,只需尝试在python控制台中导入测试用例文件即可。

例:

from project.apps.app1.tests import *

I finally figured it out working on another problem. The problem was that my test couldn’t find an import.

It looks like you get the above error if your test fails to import. This makes sense because the test suite can’t import a broken test. At least I think this is what is going on because I fixed the import within my test file and sure enough it started working.

To validate your test case just try import the test case file in python console.

Example:

from project.apps.app1.tests import *

回答 1

用:

./manage.py shell

其次是

import myapp.tests

查找导入错误的性质。

Use:

./manage.py shell

followed by

import myapp.tests

to find the nature of the import error.


回答 2

就我而言,我需要在文件夹中创建一个空的__init__.pyapp/tests

For my case, I need to create an empty __init__.py in my app/tests folder


回答 3

上面的Steve Bradshaw的示例可解决导入错误(感谢Steve)。

其他类型的错误(例如ValueError)也可能导致

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

看看这些错误是什么

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

Steve Bradshaw’s example above works for import errors (thanks Steve).

Other type of errors (e.g. ValueError) may also cause

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

to see what these errors are

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

回答 4

我和克里斯有同样的错误。我删除了一个旧模型,然后运行tests.py,但是另一个文件(views.py)仍在尝试导入已删除的模型。

当我取出现在已经过时的import语句时,问题解决了。

I had the same error as Chris. I had deleted an old model, then run tests.py, but another file (views.py) was still trying to import the deleted model.

When I took out the now-obsolete import statement, problem solved.


回答 5

确保脚本中使用的所有模块均未损坏。我的意思是在您的导入语句中检查拼写。

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

您可以通过在djano的交互式控制台中执行import语句来测试您的模块。

$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

Make sure that all modules that you are using in your script are not broken. By this I mean check spelling in your import statements.

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

You can test yours modules by executing imports statements in djano’s interactive console.

$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

回答 6

我通过修复循环导入引用解决了错误“ AttributeError:模块’utils’没有属性’name_of_my_function’”。我的文件manage.py和utils.py每个都有一个指向彼此的import语句。

I resolved the error “AttributeError: module ‘utils’ has no attribute ‘name_of_my_function’ ” by fixing a circular import reference. My files manage.py and utils.py each had an import statement pointing at each other.


回答 7

根据django文档,当运行测试时,测试实用程序的默认行为是在名称以test开头的任何文件中查找所有测试用例(即unittest.TestCase的子类),自动从中构建测试套件。这些测试用例,然后运行该套件。

所以试试这个: python manage.py test tests.py

According to django document When you run your tests, the default behavior of the test utility is to find all the test cases (that is, subclasses of unittest.TestCase) in any file whose name begins with test, automatically build a test suite out of those test cases, and run that suite.

so try this : python manage.py test tests.py


回答 8

遇到相同的错误,但检查了所有原因列表,没有解决我的问题。

最后弄清楚,原因是导入但尚未使用的一种方法的名称不正确。尽管这是一个愚蠢的错误,但它确实发生了。

Got the same error, but checked all the reasons list here, did not fix my problem.

Finally figure it out that, the reason is that the name of one method that imported but not used yet is not correct. Though it is a stupid error, it happens.


回答 9

我有同样的错误。原来是因为我将我的模块命名为common.py,但是已经有一些其他common.py模块。我要做的就是重命名我的模块。

I had the same error. It turned out to be because I named my module common.py, yet there already was some other common.py module. All I had to do was to rename my module.


回答 10

编写unittest.TestCase时遇到类似的错误。当我按原样重新键入相同的方法定义时,它似乎起作用了!

我在PyCharm上注意到的唯一变化是第二次弹出“覆盖”图标,因为setup(self)方法需要覆盖TestCase中定义的原始方法。

I had a similar error while writing a unittest.TestCase. When I re-typed the same method definition as-is, it seemed to work !

The only change I noticed on PyCharm was the ‘override’ icon pop-up the 2nd time, as the setup(self) method needs to override the original method defined in TestCase.