在 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. CSS3 之 box-shadow

    1. css3 box-shadow CSS3的box-shadow属性可以让我们轻松实现图层阴影效果 box-shadow:  inset(可选 默认没有) x-offset    y-offset ...

  2. python - socket模块1

    1.使用生活中的接打电话,解释socket通信流程  2.根据上图,写出socket通信的伪代码 2.1.server端伪代码 #买手机   #买手机卡 #开机 #等待电话 #收消息 #发消息 #挂电 ...

  3. jquery悬停tab

    <style> *{ margin:0; padding:0;} body { font:12px/19px Arial, Helvetica, sans-serif; color:#66 ...

  4. c#读写cookie

    读 response.SetCokie(new HttpCookie("Color",TextBox1.Text);写 request.Cookies["color&qu ...

  5. 《第一行代码》学习笔记14-UI(3)

    1. (1)所有控件都是直接或间接继承自View,所用的所有布局都是直接或间接继承自ViewGroup的. (2)View是Android中一种最基本的UI组件,可以在屏幕上绘制一块矩形区域,并能响应 ...

  6. trailingZeroes

    Given an integer n, return the number of trailing zeroes in n!. 给一个数字n,返回它n!数字后面有多少个0. public class ...

  7. Combination Sum,Combination Sum II,Combination Sum III

    39. Combination Sum Given a set of candidate numbers (C) and a target number (T), find all unique co ...

  8. VC:CString用法整理(转载)

    1.CString::IsEmpty BOOL IsEmpty( ) const; 返回值:如果CString 对象的长度为0,则返回非零值:否则返回0. 说明:此成员函数用来测试一个CString ...

  9. 火狐下<a>标签里嵌套的<select>不能选的bug

    今天遇到了这个问题,网上一找就找到原因了:在狐火下<a>标签里嵌套的<select>不能选 可是我查找这个问题过程中依然饶了一些时间,原因是在<a>标签没有写hre ...

  10. CSS3笔记(一)

    最开始的时候 CSS3产生的一个新属性是一个浏览器的私有的,然后W3C 可能会拿来采用做个标准,再没公布标准之前就只能用私有属性(加前缀)来表达各自厂商的实现,主要是CSS3刚出现那会儿,它暗示该CS ...