LiveServerTestCase modifies production database when ran in python script

50 views
Skip to first unread message

yakka...@gmail.com

unread,
Jun 11, 2014, 10:15:22 AM6/11/14
to django...@googlegroups.com
When I run my tests in a python script it modifies my production database.  If I run from the command line it doesn't.  When I say modify, it wipes out my existing users and replaces them with the users I create for testing. 

I followed this for the setup: https://docs.djangoproject.com/en/1.4/topics/testing/#django.test.LiveServerTestCase
I created a <setUp> to setup users. 

When I run this the production database isn't modified:
  python2.7 manage.py test yPayment

In my python script I have:
  from django.utils import unittest
  class yPaymentTest(LiveServerTestCase)
  ...
  suite_payment = unittest.TestLoader().loadTestsFromTestCase(yPaymentTest)
  unittest.TextTestRunner(verbosity=2).run(suite_payment)

Here is the full output from the command line
[brian@centos-dv7 yPaymentProj]$ /usr/local/python2.7/lib/python2.7/site-packages/virt_env/django1p4/bin/python2.7 manage.py test yPayment
Creating test database for alias 'default'...
Traceback (most recent call last):
  File "/usr/local/python2.7/lib/python2.7/wsgiref/handlers.py", line 85, in run
    self.result = application(self.environ, self.start_response)
  File "/usr/local/python2.7/lib/python2.7/site-packages/virt_env/django1p4/lib/python2.7/site-packages/django/contrib/staticfiles/handlers.py", line 67, in __call__
    return self.application(environ, start_response)
  File "/usr/local/python2.7/lib/python2.7/site-packages/virt_env/django1p4/lib/python2.7/site-packages/django/contrib/staticfiles/handlers.py", line 67, in __call__
    return self.application(environ, start_response)
  File "/usr/local/python2.7/lib/python2.7/site-packages/virt_env/django1p4/lib/python2.7/site-packages/django/core/handlers/wsgi.py", line 241, in __call__
    response = self.get_response(request)
  File "/usr/local/python2.7/lib/python2.7/site-packages/virt_env/django1p4/lib/python2.7/site-packages/django/core/handlers/base.py", line 151, in get_response
    response = self.handle_uncaught_exception(request, resolver, sys.exc_info())
  File "/usr/local/python2.7/lib/python2.7/site-packages/virt_env/django1p4/lib/python2.7/site-packages/django/core/handlers/base.py", line 226, in handle_uncaught_exception
    return callback(request, **param_dict)
  File "/usr/local/python2.7/lib/python2.7/site-packages/virt_env/django1p4/lib/python2.7/site-packages/django/utils/decorators.py", line 91, in _wrapped_view
    response = view_func(request, *args, **kwargs)
  File "/usr/local/python2.7/lib/python2.7/site-packages/virt_env/django1p4/lib/python2.7/site-packages/django/views/defaults.py", line 32, in server_error
    t = loader.get_template(template_name) # You need to create a 500.html template.
  File "/usr/local/python2.7/lib/python2.7/site-packages/virt_env/django1p4/lib/python2.7/site-packages/django/template/loader.py", line 145, in get_template
    template, origin = find_template(template_name)
  File "/usr/local/python2.7/lib/python2.7/site-packages/virt_env/django1p4/lib/python2.7/site-packages/django/template/loader.py", line 138, in find_template
    raise TemplateDoesNotExist(name)
TemplateDoesNotExist: 500.html
/usr/local/python2.7/lib/python2.7/site-packages/virt_env/django1p4/lib/python2.7/site-packages/django/db/models/fields/__init__.py:808: RuntimeWarning: DateTimeField received a naive datetime (2014-06-08 22:54:32.927908) while time zone support is active.
  RuntimeWarning)
/usr/local/python2.7/lib/python2.7/site-packages/virt_env/django1p4/lib/python2.7/site-packages/django/db/models/fields/__init__.py:808: RuntimeWarning: DateTimeField received a naive datetime (2014-06-08 22:54:32.927916) while time zone support is active.
  RuntimeWarning)
/usr/local/python2.7/lib/python2.7/site-packages/virt_env/django1p4/lib/python2.7/site-packages/django/db/models/fields/__init__.py:808: RuntimeWarning: DateTimeField received a naive datetime (2014-06-08 22:54:34.690671) while time zone support is active.
  RuntimeWarning)
.sss
----------------------------------------------------------------------
Ran 4 tests in 12.259s

OK (skipped=3)
Destroying test database for alias 'default'...



Here is the output from the python script.  
test_custom_admin_add_card (__main__.yPaymentTest) ... /usr/local/python2.7/lib/python2.7/site-packages/virt_env/django1p4/lib/python2.7/site-packages/django/db/models/fields/__init__.py:808: RuntimeWarning: DateTimeField received a naive datetime (2014-06-08 22:57:54.016573) while time zone support is active.
  RuntimeWarning)
/usr/local/python2.7/lib/python2.7/site-packages/virt_env/django1p4/lib/python2.7/site-packages/django/db/models/fields/__init__.py:808: RuntimeWarning: DateTimeField received a naive datetime (2014-06-08 22:57:54.016584) while time zone support is active.
  RuntimeWarning)
/usr/local/python2.7/lib/python2.7/site-packages/virt_env/django1p4/lib/python2.7/site-packages/django/db/models/fields/__init__.py:808: RuntimeWarning: DateTimeField received a naive datetime (2014-06-08 22:57:55.843861) while time zone support is active.
  RuntimeWarning)
ok
test_custom_admin_login (__main__.yPaymentTest) ... skipped 'skipping while debugging other functions'
test_custom_admin_no_card (__main__.yPaymentTest) ... skipped 'skipping while debugging other functions'
test_custom_admin_required_login (__main__.yPaymentTest) ... skipped 'skipping while debugging other functions'

----------------------------------------------------------------------
Ran 4 tests in 24.278s

OK (skipped=3)


I'm running the script with an IDE.

This is a similar problem: http://stackoverflow.com/questions/17435155/django-functional-liveservertestcase-after-submitting-form-with-selenium-obje
but it doesn't apply since I'm using self.live_server_url instead of hard coding a url.

I posted this to SO without a response: http://stackoverflow.com/questions/24113636/liveservertestcase-modifies-production-database-when-ran-in-python-script

yakka...@gmail.com

unread,
Jun 18, 2014, 1:08:43 PM6/18/14
to django...@googlegroups.com

The solution was to use DjangoTestSuiteRunner. I ended up copying the method DjangoTestSuiteRunner.run_tests and modifying to use a test suite.

The code is

from django.test.simple import DjangoTestSuiteRunner
from django.utils import unittest

suite_payment = unittest.TestLoader().loadTestsFromTestCase(yPaymentTest)
djangoRunner = DjangoTestSuiteRunner(verbosity=2)
djangoRunner.setup_test_environment()
old_config = djangoRunner.setup_databases()    
djangoRunner.run_suite(suite_payment )
djangoRunner.teardown_databases(old_config)
djangoRunner.teardown_test_environment()
Reply all
Reply to author
Forward
0 new messages