python - Flask-WTF 表单的单元测试验证

标签 python flask flask-wtforms wtforms

我正在为我的“注册”功能运行单元测试。有没有办法检查表单中的所有字段在提交时是否满足验证器的要求?

def test_register(self):
    form = RegistrationForm()
    form.username.data = "test"
    form.email.data = "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="691d0c1a1d290c04080005470a0604" rel="noreferrer noopener nofollow">[email protected]</a>"

我的注册表单有一个“SubmitField”,我想知道是否有一种方法(在单元测试中)模拟单击的“SubmitField”,并继续测试用户名和电子邮件字段的验证器是否满意使用“AssertTrue()”?

最佳答案

您可以调用 form.validate() 来运行字段验证,而无需模拟请求。

通常您会使用 form.validate_on_submit(),它只是 form.is_subscribed()form.validate() 的快捷方式>.

Docs

关于python - Flask-WTF 表单的单元测试验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56171561/

相关文章:

python - TypeError: must be string, not datetime.datetime 使用strptime时

python - PyParsing - 嵌套表达式和匹配标记

python - 如何在 Flask 中使用 psycopg2 (postgresql) 的连接池

flask - 表单验证失败时重定向(Flask-WTForms)

python - 定义正确的requirements.txt文件

python - 文件未在 cookiecutter-flask 应用程序中使用 Flask-wtforms 上传

python - 使用 Stanford coreNLP 在 python nltk 中进行共指解析

python - 如何在 Flask 中优雅地使用 cx_Oracle session 池?

python - 如何每天在 00 :30 AM when flask application is running? 处递增变量

python - 在 Windows 上的线程中使用 Popen 进行交互时,socketio.emit() 不起作用