首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >OpenStack中给wsgi程序写单元測试的方法

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

作者头像
全栈程序员站长
发布2022-07-08 21:05:08
发布2022-07-08 21:05:08
37800
代码可运行
举报
运行总次数:0
代码可运行

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

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

1) 使用webob生成模拟的request

代码语言:javascript
代码运行次数:0
运行
复制
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

代码语言:javascript
代码运行次数:0
运行
复制
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
代码语言:javascript
代码运行次数:0
运行
复制
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.cn/115795.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022年1月3,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档