python – AttributeError:’module’对象没有属性’tests’

我运行这个命令:

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

并导致此错误:

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

下面是我的目录结构。我也添加了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 *

转载注明原文:python – AttributeError:’module’对象没有属性’tests’ - 代码日志