zoukankan      html  css  js  c++  java
  • 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
    ']
    
        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
    ']
    
        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
    ']
    
        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()
    
            
    




  • 相关阅读:
    设计模式——观察者模式
    安卓xml动画
    部署在weblogic上的springboot项目上传文件(servlet方式)
    Spring Boot 部署到weblogic 12c
    SpingBoot+Druid监控页面打不开(404)
    DAY49-前端入门-浮动布局案例、z-index、flex布局、响应式布局、过渡与动画
    DAY48-前端入门-文档流、浮动布局、清浮动、流式布局、定位布局
    DAY46-前端入门-组合选择器、标签a_img_list、盒模型、伪类、盒模型布局
    DAY45-前端入门-css的三种引用方式以及优先级、样式与长度颜色、常用样式、css选择器
    DAY44-前端入门-前端三剑客、第一个页面、常用标签、标签分类
  • 原文地址:https://www.cnblogs.com/yxwkf/p/5244593.html
Copyright © 2011-2022 走看看