class RegisterForm(BaseForm):
phone_or_email = StringField('user', validators=[input_required()])
password = PasswordField('password',
validators=[Length(min=6, max=20),
input_required()])
captcha = StringField('captcha',
validators=[Length(min=4,max=4)])
submit = SubmitField('submit')
def validate_captcha(self, field):
if field.data != str(session['CAPTCHA_NUM']):
raise ValidationError('captcha error')
在测试代码中
def test_phone_register(self):
response = self.client.post(url_for('auth.register'),
data={
'phone_or_email' : 'no-reply@g.cc',
'password': 'google',
'captcha': 1234
})
self.assertTrue('captcha error' in response.get_data(as_text=True))
问题是在单元测试中我不知道如何获取验证码。因为catpcha在会话中,我应该在测试用户注册时加入它吗?