我想通过提交表单来对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
包的类似答案时偶然发现了这篇文章;结果他们也有一个设置。他们的文档描述了它的用法:https://github.com/praekelt/django-recaptcha - Dolan Antenucci