Flask应用单元 - 测试断言错误

时间:2012-12-21 05:30:53

标签: python unit-testing flask

我一直都是读者,但这是我第一次发帖。

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

这些是我在名为 manager.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”消息,该消息在单元测试中工作,但第二个路由获得断言错误。

这是我的测试文件 manage_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

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

1 个答案:

答案 0 :(得分:2)

您应该将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)