首页 文章

AttributeError:'module'对象没有属性'tests'

提问于
浏览
82

我正在运行此命令:

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

它会导致此错误:

AttributeError:'module'对象没有属性'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'

目录结构:

enter image description here

9 回答

  • 1

    我终于弄清楚了解另一个问题 . 问题是我的测试无法找到导入 .

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

    要验证您的测试用例,请尝试在python控制台中导入测试用例文件 .

    例:

    from project.apps.app1.tests import *
    
  • 4

    使用:

    ./manage.py shell

    其次是

    import myapp.tests

    找到导入错误的性质 .

  • 31

    对于我的情况,我需要在我的 app/tests 文件夹中创建一个 empty init.py

  • 1

    Steve Bradshaw上面的例子适用于导入错误(感谢Steve) .

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

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

    看看这些错误是什么

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

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

    当我拿出现在过时的import语句时,问题就解决了 .

  • 6

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

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

  • 11

    得到了同样的错误,但在这里检查了所有原因列表,没有解决我的问题 .

    最后弄明白,原因是导入但尚未使用的一种方法的名称不正确 . 虽然这是一个愚蠢的错误,但它发生了 .

  • 161

    确保脚本中使用的所有模块都没有损坏 . 我的意思是在你的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
    
  • 1

    我通过修复循环导入引用解决了错误“AttributeError:module'utils'没有属性'name_of_my_function'” . 我的文件manage.py和utils.py都有一个指向对方的import语句 .

相关问题