如何在django中使用验证码字段对表单进行单元测试?

luc*_*luc 9 python django captcha unit-testing

我想通过对表单进行单元测试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()
Run Code Online (Sandbox Code Playgroud)

测试代码:

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)
Run Code Online (Sandbox Code Playgroud)

是否有任何方法对这些代码进行单元测试并在测试时摆脱验证码?

提前致谢

Vin*_*rup 16

我知道这是一个旧帖子,但是django-simple-captcha现在有一个设置CAPTCHA_TEST_MODE,如果你提供值'PASSED',它会使验证码成功.您只需确保为两个验证码输入字段发送一些内容:

post_data['captcha_0'] = 'dummy-value'
post_data['captcha_1'] = 'PASSED'
self.client.post(url, data=post_data)
Run Code Online (Sandbox Code Playgroud)

CAPTCHA_TEST_MODE设置只应在测试期间使用.我的settings.py:

if 'test' in sys.argv:
    CAPTCHA_TEST_MODE = True 
Run Code Online (Sandbox Code Playgroud)


Jim*_*Gaw 7

这是我绕过它的方式.导入实际持有验证码信息的模型:

from captcha.models import CaptchaStore
Run Code Online (Sandbox Code Playgroud)

首先,我检查测试验证码表是否为空:

captcha_count = CaptchaStore.objects.count()
self.failUnlessEqual(captcha_count, 0)
Run Code Online (Sandbox Code Playgroud)

加载页面后(在这种情况下,它是一个注册页面),检查是否有新的验证码对象实例:

captcha_count = CaptchaStore.objects.count()
self.failUnlessEqual(captcha_count, 1)
Run Code Online (Sandbox Code Playgroud)

然后,我检索验证码实例数据并使用表单POST.在我的例子中,POST期望'captcha_0'包含hashkey,'captcha_1'包含响应.

captcha = CaptchaStore.objects.all()[0]
registration_data = { # other registration data here
                     'captcha_0': captcha.hashkey,
                     'captcha_1': captcha.response }
Run Code Online (Sandbox Code Playgroud)

如果在运行此测试之前启动CaptchaStore实例,则可能需要稍微调整一下.希望有所帮助.


rh0*_*ium 5

我们是这样做的。

@patch("captcha.fields.ReCaptchaField.validate")
def test_contact_view(self, validate_method):

    response = self.client.get(reverse("contact"))
    self.assertEqual(response.status_code, 200)

    data = {
        "name": "Bob Johnson",
        "email": "big_johnson@home.com",
        "phone": "800-212-2001",
        "subject": "I want Axis!",
        "message": "This is a giant\nThree liner..\nLove ya\n",
        "captcha": "XXX",
    }
    validate_method.return_value = True
    response = self.client.post(reverse("contact"), data=data)

    self.assertEqual(response.status_code, 302)
Run Code Online (Sandbox Code Playgroud)