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

Mat*_*t W 12 python paste functional-testing

我正在使用粘贴在我的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
Run Code Online (Sandbox Code Playgroud)

但我得到以下异常:

AppError: Bad response: 400 Bad Request (not 200 OK or 3xx redirect for /api/users)
Run Code Online (Sandbox Code Playgroud)

我看膏具有HttpException中间件,但我无法找到如何使用它,或者如果连正确的方式去任何例子.有什么建议?或者我只是错了?

jko*_*ker 30

我知道我对这个派对迟到了,但我跑过这个寻找同一问题的答案.要允许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
Run Code Online (Sandbox Code Playgroud)

快乐黑客!