在web.py应用上粘贴测试400个错误

时间:2011-08-15 21:48:33

标签: python functional-testing paste

我正在使用粘贴在我的web.py应用程序中对我的'控制器'进行一些功能测试。在一个案例中,我正在尝试在对API端点进行格式错误的帖子时测试400响应。以下是我的测试结果:

def test_api_users_index_post_malformed(self):
    r = self.testApp.post('/api/users', params={})
    assert r.header('Content-Type') == 'application/json'
    assert r.status == 400

但我得到以下例外:

AppError: Bad response: 400 Bad Request (not 200 OK or 3xx redirect for /api/users)

我看到paste有HttpException中间件,但是我找不到任何关于如何使用它的例子,或者它是否是正确的方法。有什么建议?或者我只是想犯这个错误?

1 个答案:

答案 0 :(得分:31)

我知道我对这个派对迟到了,但我跑过这个寻找同一问题的答案。要允许TestApp传回非2xx / 3xx响应,您需要告知请求允许“错误”。

def test_api_users_index_post_malformed(self):
    r = self.testApp.post('/api/users', params={}, expect_errors=True)
    assert r.header('Content-Type') == 'application/json'
    assert r.status == 400

快乐黑客!