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

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

大家好,又见面了,我是全栈君。

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

        

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/115795.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • mapminmax的用法

    mapminmax的用法几个要说明的函数接口:[Y,PS]=mapminmax(X)[Y,PS]=mapminmax(X,FP)Y=mapminmax(‘apply’,X,PS)X=mapminmax(‘reverse’,Y,PS)用实例来讲解,测试数据x1=[124],x2=[523];>>[y,ps]=mapminmax(x1)y=  -1

    2022年6月17日
    107
  • 常用的表单元素有哪些_h5新增的表单元素属性

    常用的表单元素有哪些_h5新增的表单元素属性这里是修真院前端小课堂,每篇分享文从【背景介绍】【知识剖析】【常见问题】【解决方案】【编码实战】【扩展思考】【更多讨论】【参考文献】八个方面深度解析前端知识/技能,本篇分享的是:【常见的表单元素有哪些?各有什么属性?】大家好,我是IT修真院北京分院第23期学员。今天小课堂的主要内容是,input表单的应用,还有在html5中新增的属性。表单元素是允许用户在表单中(比如:文…

    2025年8月12日
    3
  • Python打造最强告白代码,世界这么大,你的那个他总会遇见的(源码可直接运行)[通俗易懂]

    Python打造最强告白代码,世界这么大,你的那个他总会遇见的(源码可直接运行)[通俗易懂]能和喜欢的人在一起是一件多么美好幸福的事,可现实却并非如此,喜欢的我不敢表白,我们往往都感觉喜欢的那个人特别的优秀,也就是情人眼里出西施,即使互相都喜欢了,又有些人因羞于表达,或怕被拒绝而错过了对方,爱情就是这般捉弄人。有一首歌写得好,爱需要勇气,确实如此,希望看到这的你,能勇敢迈出第一步,勇敢追求自己的幸福,收获美好的爱情。文章末尾有惊喜。1.遇见你我很幸运❤❤要是有机会的话,我想和你一起喝奶茶,一起看电影一起吹晚风回家,一起走剩下的路,我的意思是:和你。完整源码:代码里面可以

    2022年6月2日
    41
  • Sphinx + Coreseek 实现中文分词搜索

    Sphinx + Coreseek 实现中文分词搜索

    2022年3月6日
    40
  • url加时间戳避免再次请求当前路径出现的缓存问题[通俗易懂]

    url加时间戳避免再次请求当前路径出现的缓存问题[通俗易懂]1.先解释一下,为什么要加时间戳: URL后面添加随机数通常用于防止客户端(浏览器)缓存页面。浏览器缓存是基于url进行缓存的,如果页面允许缓存,则在一定时间内(缓存时效时间前)再次访问相同的URL,浏览器就不会再次发送请求到服务器端,而是直接从缓存中获取指定资源。2.加时间戳的方法:[javascript] viewplain copy

    2022年5月1日
    220
  • Java高并发程序设计学习笔记(九):锁的优化和注意事项

    Java高并发程序设计学习笔记(九):锁的优化和注意事项

    2021年7月6日
    93

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号