Python 烧瓶应用程序单元测试断言错误

Python 烧瓶应用程序单元测试断言错误,python,unit-testing,flask,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):

我已经读了很久了,但这是我第一次发帖

好的,所以我尝试在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 
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
我想知道你们是否能帮我!我做错了什么或错过了什么

编辑

我这样做了,它起作用了


您应该将您的
hello\u用户名更改为以下内容:

> 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
编辑
根据您的评论:

> 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数据的所有要求:

def test_username(self, username):
    return self.app.get('/hello/%s' % (username), follow_redirects=True)
或者,假设你有2.6+


首先,您需要允许POST on/hello。另一方面,
username
参数到
hello\u username
不会自动将帖子数据转换为方法参数。另一方面,
t\u username
不会使用
username
参数设置帖子数据。谢谢,但是为什么我必须修改“路由”以及修复请求的方法?我知道也许是一样的。。。但这让我很困惑,如果您试图接受POST数据,那么路由需要接受POST方法。这只是HTTP。你对de POST是正确的,但是如果我更改“路由”,它将在我的应用程序中停止工作,这就是我的情况。也许我不需要发布,我遗漏了一些内容,但是idk。好的,让我们尝试一下,看看我是否混淆了你。。。如果您想对该路由(@app.route('/hello/'))进行单元测试,您会怎么做?忘了我的第一个问题吧。
@app.route('/hello/<username>', methods=['POST'])
def hello_username(username):
    print request.args
    return "Hello %s" % username
> 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
@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
def test_username(self, username):
    return self.app.get('/hello/%s' % (username), follow_redirects=True)
def test_username(self, username):
    return self.app.get('/hello/{username}'.format(username=username), follow_redirects=True)