手机版
你好,游客 登录 注册
背景:
阅读新闻

OpenStack中给wsgi程序写单元测试的方法

[日期:2014-09-01] 来源:Linux社区  作者:zhaoeryi [字体:大 中 小]

在 OpenStack 中, 针对web应用, 有三种方法来写单元测试

1) 使用webob生成模拟的request

  • from __future__ import print_function 
  • import webob 
  • import testtools 
  •  
  •  
  • def hello_world(env, start_response): 
  •     if env['PATH_INFO'] != '/': 
  •         start_response('404 Not Found', [('Content-Type', 'text/plain')]) 
  •         return ['Not Found\r\n'] 
  •  
  •     start_response('200 OK', [('Content-Type', 'text/plain')]) 
  •     return ['hello world!'] 
  •  
  • class WsgiAppTestCase(testtools.TestCase): 
  •  
  •     def test_hello_world_with_webob(self): 
  •         resp = webob.Request.blank('/').get_response(hello_world) 
  •  
  •         print("resp=%s" % (resp)) 
  •  
  •         self.assertEqual(resp.status_code, 200) 
  •         self.assertEqual(resp.body, "hello world!") 

2) 使用webtest生成模拟的request

  • from __future__ import print_function 
  • import httplib2 
  • import socket 
  • import webob 
  • import webtest 
  • import testtools 
  •  
  •  
  • def hello_world(env, start_response): 
  •     if env['PATH_INFO'] != '/': 
  •         start_response('404 Not Found', [('Content-Type', 'text/plain')]) 
  •         return ['Not Found\r\n'] 
  •  
  •     start_response('200 OK', [('Content-Type', 'text/plain')]) 
  •     return ['hello world!'] 
  •  
  •  
  • class WsgiAppTestCase(testtools.TestCase): 
  •  
  •     def test_hello_world_with_webtest(self): 
  •         app = webtest.TestApp(hello_world) 
  •         resp = app.get('/') 
  •         print("resp=%s" % (resp)) 
  •  
  •         self.assertEqual(resp.status_code, 200) 
  •         self.assertEqual(resp.body, "hello world!") 

3) 启动一个wsgi server, 并发送真实的 HTTP请求 

这种办法最复杂, 需要以下步骤 

  • 调用eventlet包, 开启monkey_patch模式
  • 使用eventlet包, 创建一个socket, 并在127.0.0.1:8080上做监听
  • 使用eventlet包, 创建一个wsgi server
  • 使用httplib2包, 发送一个HTTP数据包给server
  • from __future__ import print_function 
  • import httplib2 
  • import socket 
  • import testtools 
  •  
  •  
  • def hello_world(env, start_response): 
  •     if env['PATH_INFO'] != '/': 
  •         start_response('404 Not Found', [('Content-Type', 'text/plain')]) 
  •         return ['Not Found\r\n'] 
  •  
  •     start_response('200 OK', [('Content-Type', 'text/plain')]) 
  •     return ['hello world!'] 
  •  
  •  
  • class WsgiAppTestCase(testtools.TestCase): 
  •  
  •     def test_hello_world_with_eventlet(self): 
  •         import eventlet 
  •         eventlet.monkey_patch(os=False) 
  •  
  •         bind_addr = ("127.0.0.1","8080") 
  •         try: 
  •             info = socket.getaddrinfo(bind_addr[0], 
  •                                       bind_addr[1], 
  •                                       socket.AF_UNSPEC, 
  • socket.SOCK_STREAM)[0] 
  •             family = info[0] 
  •             bind_addr = info[-1] 
  •         except Exception: 
  •             family = socket.AF_INET 
  •  
  •         sock = eventlet.listen(bind_addr, family) 
  •              
  •         wsgi_kwargs = { 
  •             'func': eventlet.wsgi.server, 
  •             'sock': sock, 
  •             'site': hello_world, 
  •             'protocol': eventlet.wsgi.HttpProtocol, 
  •             'debug': False 
  •             } 
  •  
  •         server = eventlet.spawn(**wsgi_kwargs) 
  •          
  •         client = httplib2.Http() 
  •         resp, body = client.request( 
  •             "http://127.0.0.1:8080", "get", headers=None, body=None) 
  •         print("resp=%s, body=%s" % (resp, body)) 
  •  
  •         self.assertEqual(resp.status, 200) 
  •         self.assertEqual(body, "hello world!") 
  •          
  •         server.kill() 

在Ubuntu 12.10 上安装部署Openstack http://www.linuxidc.com/Linux/2013-08/88184.htm

Ubuntu 12.04 OpenStack Swift单节点部署手册 http://www.linuxidc.com/Linux/2013-08/88182.htm

OpenStack云计算快速入门教程 http://www.linuxidc.com/Linux/2013-08/88186.htm

企业部署OpenStack:该做与不该做的事 http://www.linuxidc.com/Linux/2013-09/90428.htm

CentOS 6.5 x64bit 快速安装OpenStack http://www.linuxidc.com/Linux/2014-06/103775.htm

本文永久更新链接地址:http://www.linuxidc.com/Linux/2014-09/106051.htm

linux
相关资讯       WSGI  OpenStack 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款