【发布时间】:2011-03-10 17:05:56
【问题描述】:
我想通过汇总表单来对 django 视图进行单元测试。问题是这个表单有一个验证码字段(基于 django-simple-captcha)。
from django import forms
from captcha.fields import CaptchaField
class ContactForm(forms.forms.Form):
"""
The information needed for being able to download
"""
lastname = forms.CharField(max_length=30, label='Last name')
firstname = forms.CharField(max_length=30, label='First name')
...
captcha = CaptchaField()
测试代码:
class ContactFormTest(TestCase):
def test_submitform(self):
"""Test that the contact page"""
url = reverse('contact_form')
form_data = {}
form_data['firstname'] = 'Paul'
form_data['lastname'] = 'Macca'
form_data['captcha'] = '28if'
response = self.client.post(url, form_data, follow=True)
有没有什么方法可以对这段代码进行单元测试并在测试时去掉验证码?
提前致谢
【问题讨论】:
-
万一其他人像我一样来到这里,我偶然发现了这篇文章,试图为
django-recaptcha包找到类似的答案;原来他们也有一个设置。他们的文档描述了它的用途:github.com/praekelt/django-recaptcha -
对于那些使用 django-recaptcha 并且需要在你的单元测试中发帖的人,你还需要像这样发送“g-recaptcha-response”: self.client.post(url, {"g -recaptcha-response": "PASSED"})
标签: python django unit-testing captcha