当我在python脚本中运行测试时,它会修改我的生产数据库。如果我从命令行运行,它不会。当我说“修改”时,它会清除我现有的用户,并用我创建的用于测试的用户替换他们。
在安装过程中,我遵循了以下步骤:https://docs.djangoproject.com/en/1.4/topics/testing/#django.test.LiveServerTestCase,我为安装用户创建了一个{##**$$}_
当我运行它时,生产数据库没有被修改: python2.7 manage.py测试yPayment
在我的python脚本中有:从django.utils导入unittest类yPaymentTest(LiveServerTestCase) .unittest.TestLoader().loadTestsFromTestCase(yPaymentTest) unittest.TextTestRunner(verbosity=2).run(suite_payment) = suite_payment
以下是命令行brian@centos-dv7yPaymentProj$ /usr/local/python2.7/lib/python2.7/site-packages/virt_env/django1p4/bin/python2.7 manage.py yPayment的完整输出,为别名‘default’创建测试数据库.回溯(最近一次调用):文件"/usr/local/python2.7/lib/python2.7/wsgiref/handlers.py",行85,在run self.result = application(self.environ,self.start_response)文件"/usr/local/python2.7/lib/python2.7/site-packages/virt_env/django1p4/lib/python2.7/site-packages/django/contrib/staticfiles/handlers.py",第67行中,在 call 返回self.application(环境环境)中,文件"/usr/local/python2.7/lib/python2.7/site-packages/virt_env/django1p4/lib/python2.7/site-packages/django/contrib/staticfiles/handlers.py",( start_response)第67行,在调用返回self.application(环境,start_response)文件的第241行,在call response =self.get_response() File 第151行中,在get_response response =self.handle_uncaught_exception(请求、解析器)中,文件"/usr/local/python2.7/lib/python2.7/site-packages/virt_env/django1p4/lib/python2.7/site-packages/django/core/handlers/base.py",第226行,在handle_uncaught_exception中返回回调(请求,**param_dict)文件"/usr/local/python2.7/lib/python2.7/site-packages/virt_env/django1p4/lib/python2.7/site-packages/django/utils/decorators.py",第91行,在文件"/usr/local/python2.7/lib/python2.7/site-packages/virt_env/django1p4/lib/python2.7/site-packages/django/views/defaults.py",第32行_wrapped_view response = view_func(request,*args,**kwargs)中,在server_error t= loader.get_template(template_name) #中,需要创建500.html模板。文件"/usr/local/python2.7/lib/python2.7/site-packages/virt_env/django1p4/lib/python2.7/site-packages/django/template/loader.py",第145行,在get_template模板中,get_template= find_template(template_name)文件"/usr/local/python2.7/lib/python2.7/site-packages/virt_env/django1p4/lib/python2.7/site-packages/django/template/loader.py",行138,在/usr/local/python2.7/lib/python2.7/site-packages/virt_env/django1p4/lib/python2.7/site-packages/django/db/models/fields/init.py:808: find_template TemplateDoesNotExist(名称) TemplateDoesNotExist: 500.html中,当时区支持处于活动状态时,RuntimeWarning: DateTimeField收到了一个天真的日期时间(2014-06-08 22:54:32.927908)。/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收到了一个天真的日期时间(2014-06-08 22:54:32.927916),而时区支持是活动的。/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收到了一个天真的日期时间(2014-06-08 22:54:34.690671),而时区支持是活动的。RuntimeWarning)
.sss
在12.259秒内进行了4次测试
OK (skipped=3)销毁测试数据库的别名‘默认’.
下面是python脚本的输出。
(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收到了一个天真的日期时间(2014-06-08 22:57:54.016573),而时区支持是活动的。/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收到了一个天真的日期时间(2014-06-08 22:57:54.016584),而时区支持是活动的。/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收到了一个天真的日期时间(2014-06-08 22:57:55.843861),而时区支持是活动的。( (main.yPaymentTest) ) RuntimeWarning)好的test_custom_admin_login .跳过“在调试其他函数时跳过”test_custom_admin_no_card test_custom_admin_no_card.跳过“在调试其他函数时跳过”test_custom_admin_required_login test_custom_admin_required_login.跳过“调试其他函数时跳过”
在24.278秒内进行了4次测试
OK (skipped=3)
我正在用IDE运行脚本。
这是一个类似的问题:Django functional LiveServerTestCase - After submitting form with selenium, objects save to non-test database,但它不适用,因为我使用的是self.live_server_url,而不是硬编码url。
我使用python2.7、django 1.4.13和Selenium的最新版本。
发布于 2014-06-18 17:07:24
解决方案是使用DjangoTestSuiteRunner。最后,我复制了方法DjangoTestSuiteRunner.run_tests并修改为使用测试套件。
代码是
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()https://stackoverflow.com/questions/24113636
复制相似问题