python - Using django.test.client to test app http requests, getting errors -


we're beginning write unit tests our api (created django rest framework). decided start off simple , use built in unittest , django.test.client classes. i've got stub of test written , runs fine:

#!/usr/bin/env python # -*- coding: utf-8 -*- __future__ import absolute_import import unittest  # 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 suite     suite.addtests(unittest.makesuite(testgrowth))      return suite  if __name__ == '__main__':     # run test suite     unittest.texttestrunner(verbosity=2).run(suite()) 

however uncomment line reading from django.test.client import client error:

traceback (most recent call last):   file "./accountgrowth.py", line 8, in <module>     django.test.client import client   file "/home/vagrant/.virtualenvs/emmasocial/lib/python2.7/site-packages/django/test/__init__.py", line 5, in <module>     django.test.client import client, requestfactory   file "/home/vagrant/.virtualenvs/emmasocial/lib/python2.7/site-packages/django/test/client.py", line 21, in <module>     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 , 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, settings not configured. must either define environment variable django_settings_module or call settings.configure() before accessing settings. 

we defining databases in settings.py follows:

if "database_url" in os.environ:     # parse database configuration $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 database file if using sqlite3.             'user': 'root',                        # not used sqlite3.             'password': '',                        # not used sqlite3.             'host': '',                            # set empty string localhost. not used sqlite3.             'port': '',                            # set empty string default. not used sqlite3.         }     } 

i'm running tests within vagrant shell inside <app>/api/tests following command python ./accountgrowth.py.

can shed light on why happening?

you're not running tests via django's test runner, sets you. don't need if __name__ == '__main__' block or explicit call run(), , should run tests via ./manage.py test.


Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -