- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我最近不得不重写我们的 rest api,并从 Flask 切换到 Cherrypy(主要是由于 Python 3 兼容性)。但是现在我一直在尝试编写我的单元测试,Flask 有一个非常漂亮的内置测试客户端,您可以使用它向您的应用程序发送虚假请求(无需启动服务器)。我找不到任何类似的功能对于 Cherrypy,是否有这样的功能,或者我是否卡在启动服务器并针对它执行实际请求?
最佳答案
据我所知,CherryPy 确实没有为这种类型的测试提供工具(没有正在运行的服务器)。但它仍然很容易做到(尽管它依赖于 CherryPy 的一些内部结构)。
这是一个简单的展示:
from StringIO import StringIO
import unittest
import urllib
import cherrypy
local = cherrypy.lib.httputil.Host('127.0.0.1', 50000, "")
remote = cherrypy.lib.httputil.Host('127.0.0.1', 50001, "")
class Root(object):
@cherrypy.expose
def index(self):
return "hello world"
@cherrypy.expose
def echo(self, msg):
return msg
def setUpModule():
cherrypy.config.update({'environment': "test_suite"})
# prevent the HTTP server from ever starting
cherrypy.server.unsubscribe()
cherrypy.tree.mount(Root(), '/')
cherrypy.engine.start()
setup_module = setUpModule
def tearDownModule():
cherrypy.engine.exit()
teardown_module = tearDownModule
class BaseCherryPyTestCase(unittest.TestCase):
def webapp_request(self, path='/', method='GET', **kwargs):
headers = [('Host', '127.0.0.1')]
qs = fd = None
if method in ['POST', 'PUT']:
qs = urllib.urlencode(kwargs)
headers.append(('content-type', 'application/x-www-form-urlencoded'))
headers.append(('content-length', '%d' % len(qs)))
fd = StringIO(qs)
qs = None
elif kwargs:
qs = urllib.urlencode(kwargs)
# Get our application and run the request against it
app = cherrypy.tree.apps['']
# Let's fake the local and remote addresses
# Let's also use a non-secure scheme: 'http'
request, response = app.get_serving(local, remote, 'http', 'HTTP/1.1')
try:
response = request.run(method, path, qs, 'HTTP/1.1', headers, fd)
finally:
if fd:
fd.close()
fd = None
if response.output_status.startswith('500'):
print response.body
raise AssertionError("Unexpected error")
# collapse the response into a bytestring
response.collapse_body()
return response
class TestCherryPyApp(BaseCherryPyTestCase):
def test_index(self):
response = self.webapp_request('/')
self.assertEqual(response.output_status, '200 OK')
# response body is wrapped into a list internally by CherryPy
self.assertEqual(response.body, ['hello world'])
def test_echo(self):
response = self.webapp_request('/echo', msg="hey there")
self.assertEqual(response.output_status, '200 OK')
self.assertEqual(response.body, ["hey there"])
response = self.webapp_request('/echo', method='POST', msg="hey there")
self.assertEqual(response.output_status, '200 OK')
self.assertEqual(response.body, ["hey there"])
if __name__ == '__main__':
unittest.main()
编辑,我已将此答案扩展为 CherryPy recipe .
关于python - 单元测试 cherrypy 网络应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14260101/
什么是 Web 服务(Rmi、ejb、soap)? Web服务和Web应用程序有什么区别?是否可以在 Web 应用程序中实现 Web 服务? 最佳答案 Web 服务是一种传输/公开信息的方式,使得可以
典型的 J2ee web 应用程序或任何基于 java 构建的 web 应用程序是多线程应用程序,所以每次我编写一些代码时我都必须牢记竞争条件或并发修改? 最佳答案 Is a typical J2ee
我正在将 google 登录集成到我的网络应用程序中。我按照 here 的说明进行操作。但我无法从登录中获取任何用户信息。不过,登录按钮工作得很好。 我使用这些 JavaScript 函数: i
关闭。这个问题需要更多 focused .它目前不接受答案。 想改进这个问题?更新问题,使其仅关注一个问题 editing this post . 4年前关闭。 Improve this questi
是否有通用 cookie 或其他方式可以检查用户是否登录到谷歌应用程序。然后,如果是这样,我想运行一些 js。如果我知道通常在 Google 应用程序/服务中使用的 cookie 的名称,我就可以读取
我正在尝试以 Web 应用程序样式展示我的记事卡。我不担心缓存或让它离线工作。 我只想让它在 iOS 浏览器中呈现良好。这是链接:http://kaninepete.com/flashcard/rev
我正在使用 bootstrap 制作响应式网络应用。 我开始在我的网站上使用导航栏和表单进行一些测试,问题是 android 中的导航栏,因为它以全宽度呈现(如桌面 View ),而 chrome(对
我正在尝试使用 http://zxing.appspot.com/scan从 WebApp 调用 Barcode Scanner,但我无法让它工作。即使在不必要地更新和重新安装之后,它所做的只是显示默
在 WebStorm 中运行 Dart Web 应用程序时,?底部的 Pane 报告以下内容(--port 因运行而异): /home/tom/dart-sdk/bin/pub serve web -
由于某些原因,我的网络应用程序的路径似乎有问题。 我在 Eclipse 中的 WEB-INF 路径如下: Project --src -- main --webapp
Apps 脚本最近已将 StackDriver 日志移至 Apps Script dashboard ,“执行”页面。 问题是,日志不会显示在 Apps 脚本 Web 应用程序的仪表板中。当我向 Ap
身份验证后,我将用户权限标识符放入用户 session 中。如何根据用户权限限制对网站某些部分的访问。现在我正在检查页面处理程序中的权限,但如何改进? 是否有任何现有的模板可以做到这一点?能举个例子吗
我打算用 GUI 前端编写一个网络应用程序,大概使用 GTK。 我对 GTK(以及一般的 GUI 编程)完全陌生。我目前的猜测是使用两个线程,一个处理网络,另一个运行 GTK 前端。 这是解决此类问题
我一直在办公室工作,我们有一些数据需要处理几次。我的意思是数百行,有时每行中有非常大的文本 block 。为我们的客户说出食谱,其中包含 ID、名称、类别、食谱本身、时间…… 问题是我们需要经常处理和
当谈到使用官方 web SDK 的 Firestore 缓存时,它是否优化了读取以便仅当文档在上次读取后发生更改时才向服务器“发送”读取请求? (因此每次尝试不会生成 1 次额外的文档读取) 为了详细
我开发了一个使用 Jquery 移动框架的移动网络应用程序...我的主页我添加了一个 div,其中包含一些关于谁浏览我的网站 android 设备的说明... Some instructi
我就是这么想的,不知道是不是我 react 慢了。 通常,我将正在编辑的项目的 ID 存储在隐藏字段中。然后在后端(顺便说一句,我正在使用 PHP/Zend Framework),我用它来确定要编辑的
我有一些应用创意想免费发布(带广告)。我是一名 Web 开发人员,目前不想学习 Java/Objective C。我可以轻松地将想法构建到 HTML 5 在线应用程序中。 有什么原因我不能使用 Pho
我正在编写一个网络套接字应用程序,我打算使用 Azure 网络应用程序将其托管在云上。 Web 套接字是使用相当标准的 Owin 中间件实现的,并且在前 100 秒内功能齐全。这段时间之后,webso
是否有任何协议(protocol)可用于在(现在可下载的)谷歌地图中打开地址?这不是 native 应用程序,只是 Safari 的网络应用程序/网站(网络剪辑)。 Google 是否提供任何此类协议
我是一名优秀的程序员,十分优秀!