在 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 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()

OpenStack中给wsgi程序写单元測试的方法的更多相关文章

  1. 在Eclipse中使用JUnit4进行单元測试(0基础篇)

    本文绝大部分内容引自这篇文章: http://www.devx.com/Java/Article/31983/0/page/1 我们在编写大型程序的时候,须要写成千上万个方法或函数,这些函数的功能可能 ...

  2. php单元測试

    你是否在程序开发的过程中遇到下面的情况:当你花了非常长的时间开发一个应用后,你觉得应该是大功告成了,可惜在调试的时候,老是不断的发现bug,并且最可怕的是,这些bug是反复出现的,你可能发现这些bug ...

  3. 玩转单元測试之WireMock -- Web服务模拟器

    WireMock 是一个灵活的库用于 Web 服务測试,和其它測试工具不同的是.WireMock 创建一个实际的 HTTPserver来执行你的 Web 服务以方便測试. 它支持 HTTP 响应存根. ...

  4. SonarQube4.4+Jenkins进行代码检查实例之三-单元測试分析

    作者:张克强    作者微博:张克强-敏捷307 在 <SonarQube4.4+Jenkins进行代码检查实例之中的一个> 中介绍了不编译仅仅检查的方式. 在<SonarQube4 ...

  5. 让你提前认识软件开发(19):C语言中的协议及单元測试演示样例

    第1部分 又一次认识C语言 C语言中的协议及单元測试演示样例 [文章摘要] 在实际的软件开发项目中.常常要实现多个模块之间的通信.这就须要大家约定好相互之间的通信协议,各自依照协议来收发和解析消息. ...

  6. maven多module项目中千万不要引入其它模块的单元測试代码

    本文出处:http://blog.csdn.net/chaijunkun/article/details/35796335,转载请注明. 因为本人不定期会整理相关博文,会对对应内容作出完好. 因此强烈 ...

  7. Android studio及eclipse中的junit单元測试

    转载请标明出处:http://blog.csdn.net/nmyangmo/article/details/51179106 前一段时间有人问我单元測试的相关内容,我稍作总结做日志例如以下: 由于我接 ...

  8. 单元測试中 Right-BICEP 和 CORRECT

    My Blog:http://www.outflush.com/ 在单元測试中,有6个总结出的值得測试的方面,这6个方面统称为 Right-BICEP.通过这6个方面的指导.能够较全然的測试出代码中的 ...

  9. C语言单元測试

    C语言单元測试 对于敏捷开发来说,单元測试不可缺少,对于Java开发来说,JUnit非常好,对于C++开发,也有CPPUnit可供使用,而对于传统的C语言开发,就没有非常好的工具可供使用,能够找到的有 ...

随机推荐

  1. [Redux] Extracting Container Components (FilterLink)

    Learn how to avoid the boilerplate of passing the props down the intermediate components by introduc ...

  2. Codeforces Round #311 (Div. 2) E - Ann and Half-Palindrome(字典树+dp)

    E. Ann and Half-Palindrome time limit per test 1.5 seconds memory limit per test 512 megabytes input ...

  3. vim的用法

    1. vi 与 vim 有什么区别呢,它们之间有什么关系?Vim是从Vi发展出来的一个文本编辑器,可以看作是vi的升级版.Vim的主要功能与原始的Vi完全兼容,vi不会显示颜色,而vim会根据文件内容 ...

  4. linux下mysql环境支持中文配置步骤

    sql脚本执行前加上: CREATE DATABASE IF NOT EXISTS mydatabase DEFAULT CHARSET utf8 COLLATE UTF8_GENERAL_CI; u ...

  5. sqlserver2005重新安装(安装汇编错误,安装程序无法连接到数据库服务进行服务配置)

    2014-01-09 16:41 1687人阅读 评论(1) 收藏 举报 分类: 数据库(1) 版权声明:本文为博主原创文章,未经博主允许不得转载. sqlserver2005重新安装(安装汇编错误, ...

  6. HTML5开发入门经典教程和案例合集(含视频教程)

    HTML5作为下一代网页语言,对Web开发者而言,是一门必修课.本文档收集了多个HTML5经典技术文档(HTML5入门资料.经典)以及游戏开发案例以及教学视频等,帮助同学们掌握这门重要的技术. 资源名 ...

  7. CODEVS 1062 路由选择

    1062 路由选择  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 钻石 Diamond 题目描述 Description 在网络通信中,经常需要求最短路径.但完全用最短路径传 ...

  8. android如何保存读取读取文件文件保存到SDcard

    android如何保存读取读取文件文件保存到SDcard 本文来源于www.ifyao.com禁止转载!www.ifyao.com 上图为保存文件的方法体. 上图为如何调用方法体保存数据. 上面的截图 ...

  9. IntelliJ IDEA14如何配置tomcat

    http://doc.okbase.net/frank1234/archive/121479.html

  10. jQuery选择器的学习

    jQuery的核心在于它的选择器,通过观看视频和阅读,发现jQuery选择器大体上的分类可分为这么几种(不同人方式不同,这里选择一个自认为比较好的): 1.基础选择器(对应api文档中的基本选择器和层 ...