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

alb*_*ogg 6 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 
Run Code Online (Sandbox Code Playgroud)

第一个路由是加载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
Run Code Online (Sandbox Code Playgroud)

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

.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)
Run Code Online (Sandbox Code Playgroud)

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

编辑

我这样做了,它正在发挥作用

 
    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
Run Code Online (Sandbox Code Playgroud)

sbe*_*rry 2

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

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

确保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
Run Code Online (Sandbox Code Playgroud)

你的测试应该是这样的:

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

编辑
根据您的评论:

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

但是,我不知道为什么要使用 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
Run Code Online (Sandbox Code Playgroud)

在这种情况下,我将删除对 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
Run Code Online (Sandbox Code Playgroud)

使用 GET 的测试是

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

或者,假设您有 2.6+,

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