Flask app.post失败

时间:2015-12-23 20:03:18

标签: python unit-testing flask flask-login

我正在尝试为Flask应用程序编写一些单元测试,因为我是Flask的新手。我试图测试登录功能是否正常工作。我的相关单元测试代码如下所示:

def login(self, name, password):
    return self.app.post('/', data={
        'password': password, 'name': name}, 
        follow_redirects=True)

def test_users_cannot_login_unless_registered(self):
    response = self.login('foo', 'bar')
    self.assertIn(b'Invalid username or password.', response.data)

当我手动测试应用时,收到“'无效的用户名或密码”的错误消息。但是,当我运行单元测试时,我得到以下失败:

Traceback (most recent call last):
  File "tests.py", line 55, in test_users_cannot_login_unless_registered
self.assertIn(b'Invalid username or password.', response.data)
AssertionError: 'Invalid username or password.' not found in '<!DOCTYPE html>\n<html>\n<head>\n\t<title>Welcome to FlaskTaskr!</title>\n\t<link rel="stylesheet" href="/static/css/main.css">\n\t</head>\n\t<body>\n\t\t<div class="page">\n\n\t\t\t\n\n\t\t\t\n\t\t\t<div class="error"><strong>ERROR:</strong> Both fields are required.</div>\n\t\t\t\n\n\t\t\t<br>\n\n\t\t\t\n\n<h1>Welcome to FlaskTaskr.</h1>\n<div class="lead">Please sign in to access your task list.</div>\n<form class="form-signin" role="form" method="post" action="/">\n\t<input id="csrf_token" name="csrf_token" type="hidden" value="1450903611##20d216a4c9ad935bc78678f060bfd898a7ead69f">\n\t<p>\n\t\t<input id="name" name="name" placeholder="name" type="text" value="foofoo">\n\t\t<span class="error">\n\t\t\t\n\t\t</span>\n\t</p>\n\t<p>\n\t\t<input id="password" name="password" placeholder="password" type="password" value="">\n\t\t<span class="error">\n\t\t\t\n\t\t</span>\n\t</p>\n\t<button class="btn btn-sm btn-success" type="submit">Sign in</button>\n\t<br>\n\t<br>\n\t<p><em>Need an account? </em><a href="/register">Sign up!</a></p>\n</form>\n\n\n\n\t\t</div>\n\t</body>\n\t</html>'

文本的存在&#39;两个字段都是必需的&#39;,似乎告诉我密码没有传递给应用程序。这是我用来生成页面的html / jinja2代码:

{% extends "_base.html" %}
{% block content %}

<h1>Welcome to FlaskTaskr.</h1>
<div class="lead">Please sign in to access your task list.</div>
<form class="form-signin" role="form" method="post" action="/">
  {{ form.csrf_token }}
  <p>
    {{ form.name(placeholder="name") }}
    <span class="error">
      {% if form.name.errors %}
      {% for error in form.name.errors %}
      {{ error }}
      {% endfor %}
      {% endif %}
    </span>
  </p>
  <p>
    {{ form.password(placeholder="password") }}
    <span class="error">
      {% if form.password.errors %}
      {% for error in form.password.errors %}
      {{ error }}
      {% endfor %}
      {% endif %}
    </span>
  </p>
  <button class="btn btn-sm btn-success" type="submit">Sign in</button>
  <br>
  <br>
  <p><em>Need an account? </em><a href="/register">Sign up!</a></p>
</form>
{% endblock %}

关于问题可能是什么或在哪里寻找问题的任何想法?

谢谢!

1 个答案:

答案 0 :(得分:3)

我正在回答我自己的问题。答案可以在这里找到:

Flask-WTF / WTForms with Unittest fails validation, but works without Unittest

问题是我已经设置了

app.config['WTF_CSRF_ENABLED'] = True

在我的设置中。它需要设置为False:

app.config['WTF_CSRF_ENABLED'] = False

一旦完成,测试就有效了。