Django Web 应用程序测试

发布于 2024-10-30 02:24:20 字数 2652 浏览 0 评论 0原文


我在tests.py中有以下内容。

def setUp(self):
    self.client = Client()
    self.client.get('/homepage',{'join':'NPO2','siteid':1450})
    self.client.session.save()
    self.oraganisation_list = ['NPO1','NPO2','NPO3']
    self.pay_recursion_list = ['annual','monthly','bi-annual','quarter']
def test_paytermpage(self):
    for org in self.organisation_list:
        response = self.client.get('',{'join':org,'siteid':1450})
        self.failUnlessEqual(response.status_code,200)
        self.assertTemplateUsed(response,'some.html')
def test_infopage(self):
    for term in self.pay_recurstion_list:
        response = self.client.post('',{'pay-term':term,'submit':'payterm'})
        self.failUnlessEqual(response.status_code,200)

test_infopage() 失败,这是回溯。

Traceback (most recent call last):
  File "/var/lib/django/bsdata/shoppingcart/tests.py", line 50, in test_infopage
    response = self.client.post('',{'pay-term':term,'submit':'payterm'})
  File "/usr/lib/pymodules/python2.6/django/test/client.py", line 313, in post
    response = self.request(**r)
  File "/usr/lib/pymodules/python2.6/django/core/handlers/base.py", line 92, in get_response
    response = callback(request, *callback_args, **callback_kwargs)
  File "/var/lib/django/.../views.py", line 22, in start
    term,costdict,webobj = costInfo(request)
  File "/var/lib/django/...views.py", line 238, in getCostInfo
    cost_dict = Site.objects.getDict(request.session['siteid']) 
  File "/var/lib/django/.../managers.py", line 16, in getLoadedDict
    siteobj = Site.objects.get(pk=agent)
  File "/usr/lib/pymodules/python2.6/django/db/models/manager.py", line 120, in get
    return self.get_query_set().get(*args, **kwargs)
  File "/usr/lib/pymodules/python2.6/django/db/models/query.py", line 305, in get
    % self.model._meta.object_name)
DoesNotExist: Site matching query does not exist.

我进行了调试以查看 siteobj = Site.objects.get(pk=agent) 中的“agent”值正在获取有效整数。

令人惊讶的是,当从这样的 shell 进行测试时,它们都可以工作

setup_test_environment()
client = Client()
client.get('/shoppingcart',{'join':'NPO1','siteid':1450})
client.session.save()
oraganisation_list = ['NPO1','NPO2','NPO3']
pay_recursion_list = ['annual','monthly','bi-annual','quarter']
for org in oraganisation_list:
    response = client.get('',{'join':org,'siteid':1450})
    TestCase.failUnlessEqual(t,response.status_code,200)
for term in pay_recursion_list:
    response = client.post('',{'pay-term':term,'submit':'payterm'})
    TestCase.failUnlessEqual(t,response.status_code,200)

。抱歉,信息太多,不知道如何更好地解释。 任何想法都会对这个新手非常有帮助。谢谢。

I have the following in tests.py.

def setUp(self):
    self.client = Client()
    self.client.get('/homepage',{'join':'NPO2','siteid':1450})
    self.client.session.save()
    self.oraganisation_list = ['NPO1','NPO2','NPO3']
    self.pay_recursion_list = ['annual','monthly','bi-annual','quarter']
def test_paytermpage(self):
    for org in self.organisation_list:
        response = self.client.get('',{'join':org,'siteid':1450})
        self.failUnlessEqual(response.status_code,200)
        self.assertTemplateUsed(response,'some.html')
def test_infopage(self):
    for term in self.pay_recurstion_list:
        response = self.client.post('',{'pay-term':term,'submit':'payterm'})
        self.failUnlessEqual(response.status_code,200)

test_infopage() is failing and here is the traceback.

Traceback (most recent call last):
  File "/var/lib/django/bsdata/shoppingcart/tests.py", line 50, in test_infopage
    response = self.client.post('',{'pay-term':term,'submit':'payterm'})
  File "/usr/lib/pymodules/python2.6/django/test/client.py", line 313, in post
    response = self.request(**r)
  File "/usr/lib/pymodules/python2.6/django/core/handlers/base.py", line 92, in get_response
    response = callback(request, *callback_args, **callback_kwargs)
  File "/var/lib/django/.../views.py", line 22, in start
    term,costdict,webobj = costInfo(request)
  File "/var/lib/django/...views.py", line 238, in getCostInfo
    cost_dict = Site.objects.getDict(request.session['siteid']) 
  File "/var/lib/django/.../managers.py", line 16, in getLoadedDict
    siteobj = Site.objects.get(pk=agent)
  File "/usr/lib/pymodules/python2.6/django/db/models/manager.py", line 120, in get
    return self.get_query_set().get(*args, **kwargs)
  File "/usr/lib/pymodules/python2.6/django/db/models/query.py", line 305, in get
    % self.model._meta.object_name)
DoesNotExist: Site matching query does not exist.

I did debug to see what value 'agent' in siteobj = Site.objects.get(pk=agent) is getting its a valid integer.

Surprisingly both of them are working when tested from shell like this

setup_test_environment()
client = Client()
client.get('/shoppingcart',{'join':'NPO1','siteid':1450})
client.session.save()
oraganisation_list = ['NPO1','NPO2','NPO3']
pay_recursion_list = ['annual','monthly','bi-annual','quarter']
for org in oraganisation_list:
    response = client.get('',{'join':org,'siteid':1450})
    TestCase.failUnlessEqual(t,response.status_code,200)
for term in pay_recursion_list:
    response = client.post('',{'pay-term':term,'submit':'payterm'})
    TestCase.failUnlessEqual(t,response.status_code,200)

Sorry for too much info,didn't know how to explain better.
Any ideas would be highly helpful for this newbie. Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

北恋 2024-11-06 02:24:20

Django 测试运行程序使用与生产数据不同的数据库。如果您的站点需要存在一个站点,您应该将其添加到您的测试设置中,或者您应该需要一个加载该站点的固定装置。

The Django test runner uses a different database than your production data. If your site requires a Site to exist, you should either add it in your Test setUp, or you should require a fixture that loads the site.

我一向站在原地 2024-11-06 02:24:20

尝试将“SITE_ID = 1”放入您的设置中。这是站点框架的一部分。

Try putting "SITE_ID = 1" in your settings. This is part of the sites framework.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文