2013-05-02 96 views
3

我們開始爲我們的API編寫單元測試(使用Django Rest Framework創建)。我們決定從簡單的開始,使用內置的unittest和django.test.client類。我有寫一個測試的存根,它運行得很好:只要我去掉這行:from django.test.client import Client我得到一個錯誤使用django.test.client測試應用http請求,獲取錯誤

#!/usr/bin/env python 
# -*- coding: utf-8 -*- 
from __future__ import absolute_import 
import unittest 

# from django.test.client import Client 

class TestGrowth(unittest.TestCase): 
    def setUp(self): 
     # self.client = Client() 
     pass 

    def test_numbers_3_4(self): 
     self.assertEqual(4, 4, True) 

    def test_strings_a_3(self): 
     self.assertEqual('andy', 'andy', True) 


def suite(): 
    suite = unittest.TestSuite() 

    # Add test cases to suite 
    suite.addTests(unittest.makeSuite(TestGrowth)) 

    return suite 

if __name__ == '__main__': 
    # Run test suite 
    unittest.TextTestRunner(verbosity=2).run(suite()) 

但是:

Traceback (most recent call last): 
    File "./AccountGrowth.py", line 8, in <module> 
    from django.test.client import Client 
    File "/home/vagrant/.virtualenvs/emmasocial/lib/python2.7/site-packages/django/test/__init__.py", line 5, in <module> 
    from django.test.client import Client, RequestFactory 
    File "/home/vagrant/.virtualenvs/emmasocial/lib/python2.7/site-packages/django/test/client.py", line 21, in <module> 
    from django.db import close_connection 
    File "/home/vagrant/.virtualenvs/emmasocial/lib/python2.7/site-packages/django/db/__init__.py", line 11, in <module> 
    if settings.DATABASES and DEFAULT_DB_ALIAS not in settings.DATABASES: 
    File "/home/vagrant/.virtualenvs/emmasocial/lib/python2.7/site-packages/django/conf/__init__.py", line 53, in __getattr__ 
    self._setup(name) 
    File "/home/vagrant/.virtualenvs/emmasocial/lib/python2.7/site-packages/django/conf/__init__.py", line 46, in _setup 
    % (desc, ENVIRONMENT_VARIABLE)) 
django.core.exceptions.ImproperlyConfigured: Requested setting DATABASES, but settings are not configured. 
You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings. 

我們定義在數據庫中settings.py如下:

if "DATABASE_URL" in os.environ: 
    # Parse database configuration from $DATABASE_URL 
    DATABASES = { 
     'default': dj_database_url.config() 
    } 
else: 
    DATABASES = { 
     'default': { 
      'ENGINE': 'django.db.backends.mysql', # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'. 
      'NAME': 'emmasocial',     # Or path to database file if using sqlite3. 
      'USER': 'root',      # Not used with sqlite3. 
      'PASSWORD': '',      # Not used with sqlite3. 
      'HOST': '',       # Set to empty string for localhost. Not used with sqlite3. 
      'PORT': '',       # Set to empty string for default. Not used with sqlite3. 
     } 
    } 

我從遊民外殼中使用以下命令python ./AccountGrowth.py運行裏面<app>/api/tests測試。

任何人都可以闡明爲什麼會發生這種情況?

回答

1

您並未通過Django的測試運行器運行測試,該測試運行器爲您設置了所有這些測試運行器。您不需要if __name__ == '__main__'區塊或明確呼叫run(),您應該通過./manage.py test運行測試。

+1

丹尼爾...目標是將這些作爲大型套件的一部分來運行,但我只是開始進行單元測試。當我試着用'./manage.py test'從項目的根目錄運行它時,它似乎沒有相同的表現。 – commadelimited 2013-05-02 20:29:30

相關問題