【问题标题】:Flask Application Unit-Test Assertion ErrorFlask 应用程序单元测试断言错误
【发布时间】:2012-12-21 05:30:53
【问题描述】:

我已经阅读了很长时间,但这是我第一次发帖。

好的,所以我正在尝试在 Flask 中对演示应用程序进行单元测试,但我不知道自己做错了什么。

这些是我在一个名为 ma​​nager.py 的文件中的“路线”:

@app.route('/')
@app.route('/index')
def hello():
    return render_template('base.html')


@app.route('/hello/<username>')
def hello_username(username):
    return "Hello %s" % username 

第一个路由正在加载 base.html 模板呈现“hi”消息,该消息在单元测试中有效,但第二个路由收到断言错误。

这是我的测试文件ma​​nage_test.py

class ManagerTestCase(unittest.TestCase):

    def setUp(self):
        self.app = app.test_client()

    def t_username(self, username):
        return self.app.post('/hello/<username>', follow_redirects=True)

    def test_username(self):
        rv = self.t_username('alberto')
        assert "Hello alberto" in rv.data

    def test_empty_db(self):
        rv = self.app.get('/')
        assert 'hi' in rv.data

这是单元测试运行的输出:

.F
======================================================================
FAIL: test_username (tests.manage_tests.ManagerTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Users/albertogg/Dropbox/code/Python/flask-bootstrap/tests/manage_tests.py", line 15, in test_username
    assert "Hello alberto" in rv.data
AssertionError

----------------------------------------------------------------------
Ran 2 tests in 0.015s

FAILED (failures=1)

我想知道你们是否可以帮助我!我做错了什么或错过了什么?

编辑

我这样做了,它正在工作

 
    class ManagerTestCase(unittest.TestCase):

    def setUp(self):
        self.app = app.test_client()

    def t_username(self, username):
        return self.app.get('/hello/%s' % (username), follow_redirects=True')
        # either that or the Advanced string formatting from the answer are working.

    def test_username(self):
        rv = self.t_username('alberto')
        assert "Hello alberto" in rv.data

    def test_empty_db(self):
        rv = self.app.get('/')
        assert 'hi' in rv.data

【问题讨论】:

  • 一方面,您需要在 /hello 上允许 POST。另一方面,hello_usernameusername 参数不会自动将 POST 数据转换为方法参数。
  • 另外,t_username 不会使用username 参数设置帖子的数据。

标签: python unit-testing flask


【解决方案1】:

您应该将hello_username 更改为以下内容:

@app.route('/hello/', methods=['POST'])
def hello_username():
    return "Hello %s" % request.form.get('username', 'nobody')

确保也from flask import request

还有一个例子,展示它的工作原理:

> curl -X POST -i 'http://localhost:2000/hello/' -d "username=alberto"
HTTP/1.0 200 OK
Content-Type: text/html; charset=utf-8
Content-Length: 9
Server: Werkzeug/0.8.3 Python/2.7.2
Date: Fri, 21 Dec 2012 05:42:49 GMT

Hello alberto

你的测试应该是这样的:

def test_username(self, username):
    return self.app.post('/hello', data={"username":username})

编辑
根据您的评论:

@app.route('/hello/<username>', methods=['POST'])
def hello_username(username):
    print request.args
    return "Hello %s" % username

但是,我不知道您为什么要使用 POST,因为这本质上是一个没有任何 POST 正文的 POST。

> curl -X POST -i 'http://localhost:2000/hello/alberto'           
HTTP/1.0 200 OK
Content-Type: text/html; charset=utf-8
Content-Length: 13
Server: Werkzeug/0.8.3 Python/2.7.2
Date: Fri, 21 Dec 2012 06:29:25 GMT

Hello alberto

在这种情况下,我会一起删除对 POST 数据的要求:

@app.route('/hello/<username>', methods=['POST'])
def hello_username(username):
    print request.args
    return "Hello %s" % username


> curl -i 'http://localhost:2000/hello/alberto'           
HTTP/1.0 200 OK
Content-Type: text/html; charset=utf-8
Content-Length: 13
Server: Werkzeug/0.8.3 Python/2.7.2
Date: Fri, 21 Dec 2012 06:31:10 GMT

使用 GET 的测试是

def test_username(self, username):
    return self.app.get('/hello/%s' % (username), follow_redirects=True)

或者,假设你有 2.6+,

def test_username(self, username):
    return self.app.get('/hello/{username}'.format(username=username), follow_redirects=True)

【讨论】:

  • 谢谢,但是为什么我必须修改“路由”和方法才能修复请求?我知道这可能是同一件事......但它让我很困惑
  • 如果你尝试接受 POST 数据,那么路由需要接受 POST 方法。它只是 HTTP。
  • de POST 是对的,但如果我更改“路线”,它将在我的应用程序中停止工作,这就是我的情况
  • 也许我不需要做 POST 并且我遗漏了一些东西,但是 idk。
  • 好的,让我们试试看我是否让你感到困惑......如果你想对该路由进行单元测试 (@app.route('/hello/')) 会怎样你做?忘记我的第一个问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-05
  • 2021-04-14
  • 1970-01-01
  • 1970-01-01
  • 2018-02-20
  • 1970-01-01
相关资源
最近更新 更多