Python 在web.py应用程序上使用粘贴测试400个错误
我正在使用paste对我的web.py应用程序中的“控制器”进行一些功能测试。在一个例子中,当向API端点发送格式错误的post时,我尝试测试400响应。以下是我的测试结果:Python 在web.py应用程序上使用粘贴测试400个错误,python,functional-testing,paste,Python,Functional Testing,Paste,我正在使用paste对我的web.py应用程序中的“控制器”进行一些功能测试。在一个例子中,当向API端点发送格式错误的post时,我尝试测试400响应。以下是我的测试结果: def test_api_users_index_post_malformed(self): r = self.testApp.post('/api/users', params={}) assert r.header('Content-Type') == 'application/json' as
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中间件,但我找不到任何关于如何使用它或者它是否正确的例子。有什么建议吗?还是我只是做错了?我知道我参加聚会迟到了,但我在寻找同一问题的答案时遇到了这个问题。要允许TestApp传回非2xx/3xx响应,您需要告诉请求允许“错误”
快乐的黑客 看起来您需要捕捉错误。尝试将测试设置为
unittest.TestCase
,并使用其assertRaises
方法:我认为这无助于测试我的响应。异常是在self.testApp.post(…)调用期间引发的,因此我无法检查我的状态代码这是完美的。谢谢
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