Tim*_*Tim -1 python google-app-engine unit-testing http webtest
我在python中使用Google App Engine设置项目.
目前它看起来像这样
class MainPage(webapp2.RequestHandler):
def get(self):
    self.response.headers['Content-Type'] = 'text/plain'
    self.response.write('Hello World!')
application = webapp2.WSGIApplication([
    ('/', MainPage),
], debug=True)
我试图学习如何使用TDD方式,所以我通过Google 测试了get以下这个例子.
它有这个测试用例
def test_MainPage_get(self):
    response = self.testapp.get('/')
    self.assertEqual(response.status_int, 200)
效果很好,按预期返回200.然后我想我也应该测试post一下.我试着像这样测试它
def test_MainPage_post(self):
    response = self.testapp.post('/')
    self.assertEqual(response.status_int, 405)
因为post没有实现,我希望它返回状态405和测试用例来报告成功.然而,控制台显示此并退出
The method POST is not allowed for this resouce.
------------------------------------------------
Ran 2 tests in 0.003s
FAILED (errors=1)
为什么它停在那里而不是将405返回到我的测试用例?我做错了吗?是否有其他(更好)的方法来测试method not allowed代码?
一个例外被升高为不是一个2xx或3xx状态代码的任何响应.
你断言它正在被提升:
def test_MainPage_post(self):
    with self.assertRaises(webtest.AppError) as exc:
        response = self.testapp.post('/')
    self.assertTrue(str(exc).startswith('Bad response: 405')
或者,设置expect_errors为True:
def test_MainPage_post(self):
    response = self.testapp.post('/', expect_errors=True)
    self.assertEqual(response.status_int, 405)
或告诉post方法期望405:
def test_MainPage_post(self):
    response = self.testapp.post('/', status=405)
AppError如果响应状态不是405 ,那么将在哪里引发.status这里也可以是状态列表或元组.
| 归档时间: | 
 | 
| 查看次数: | 972 次 | 
| 最近记录: |