2014-08-29 63 views
75

我運行這個命令:AttributeError的: '模塊' 對象有沒有屬性 '測試'

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' 

目錄結構:

enter image description here

+0

沒有'project':'python manage.py test apps.app1.tests'嗎? – alecxe 2014-08-29 19:37:59

+0

不,我得到ImportError:沒有名爲應用程序的模塊 – Chris 2014-08-29 19:44:31

回答

146

我終於想通了對另一個問題的工作。問題是我的測試找不到導入。

它看起來像你得到上述錯誤,如果你的測試無法導入。這是有道理的,因爲測試套件不能導入破壞的測試。至少我認爲這是發生了什麼,因爲我在我的測試文件中修復了導入,並確信它已經開始工作。

要驗證您的測試用例,只需在python控制檯中導入測試用例文件即可。

例子:

from project.apps.app1.tests import * 
+10

喔,謝謝,它是如此不可能找到錯誤.. – 2014-11-02 23:37:01

+0

非常感謝!對於我來說,第三方模塊「springpython」缺失。 – Alex 2015-11-10 09:46:18

+0

該消息中的錯誤假設。 – AdamC 2016-01-25 22:10:04

0

根據Django的文檔時run your tests,測試工具的默認行爲是要找到所有的測試用例(即,unittest.TestCase子類)中任何名稱以test開頭的文件,會自動從這些測試用例中構建一個測試套件,並運行該套件。

所以試試這個:python manage.py test tests.py

4

我有同樣的錯誤克里斯。我刪除了舊的模型,然後運行tests.py,但另一個文件(views.py)仍在嘗試導入已刪除的模型。

當我拿出現在已過時的導入語句時,問題就解決了。

28

用途:

./manage.py shell

其次

import myapp.tests

找到導入錯誤的性質。

5

Steve Bradshaw上面的示例適用於導入錯誤(感謝Steve)。

錯誤(例如ValueError異常)的其他類型也可能會導致

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

,看看這些錯誤是

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

對於我來說,我需要創建一個空的__init__。py在我的app/tests文件夾中

0

得到了同樣的錯誤,但在這裏檢查了所有的原因列表,沒有解決我的問題。

最後弄明白,原因是導入但尚未使用的一種方法的名稱不正確。雖然這是一個愚蠢的錯誤,但它會發生。

0

確保您在腳本中使用的所有模塊均未損壞。我的意思是在你的導入語句中檢查拼寫。

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

您可以通過在djano的交互式控制檯中執行imports語句來測試您的模塊。

[email protected]: 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 
相關問題