每当我想在我的代码中测试404 HTTP错误路径时,我都会收到以下错误:
AssertionError:Content-Length与实际的app_iter长度不同(512!= 60)
我创建了一个触发此行为的最小示例:
import unittest
import endpoints
from protorpc import remote
from protorpc.message_types import VoidMessage
import webtest
@endpoints.api(name='test', version='v1')
class HelloWorld(remote.Service):
@endpoints.method(VoidMessage, VoidMessage,
path='test_path', http_method='POST',
name='test_name')
def test(self, request):
raise endpoints.NotFoundException("Not found")
class AppTest(unittest.TestCase):
def setUp(self):
app = endpoints.api_server([HelloWorld])
self.testapp = webtest.TestApp(app)
# Test the handler.
def testHelloWorldHandler(self):
response = self.testapp.post('/_ah/spi/HelloWorld.test', extra_environ={
'SERVER_SOFTWARE': 'Development/X', 'CONTENT_TYPE': 'application/json'})
Run Code Online (Sandbox Code Playgroud)
那么我做错了什么?
有什么方法可以测试使用JUnit或其他框架使用Google Cloud Endpoint编写的API?
在文档中,有一个使用curl命令的示例,其背后的逻辑也许是仅在客户端上测试API。
当我尝试找到一些方法来从服务器端测试API时,遇到了编写JUnit测试并将HttpURLConnection调用到localhost 的可能性,但是这种方法存在问题。例如,应用引擎的实例应该在测试之前已经运行,但是我使用maven在本地部署,并且测试是在部署之前,所以如果测试失败,则它不会部署dev服务器,我不认为这是正确的重写Maven步骤的方法。
编辑1:发现与Python类似的方法:如何对Google Cloud Endpoints进行单元测试