AttributeError:'module'对象没有属性'tests' [英] AttributeError: 'module' object has no attribute 'tests'

查看:526
本文介绍了AttributeError:'module'对象没有属性'tests'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在运行此命令:

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

,它会导致此错误:


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

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

下面是我的目录结构。我也添加了app1到我安装的应用程序配置。

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'

目录结构:

推荐答案

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

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.

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

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

示例:

from project.apps.app1.tests import *

这篇关于AttributeError:'module'对象没有属性'tests'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆