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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • python 两个list 求交集,并集,差集

    python 两个list 求交集,并集,差集在python中,数组可以用list来表示。如果有两个数组,分别要求交集,并集与差集,怎么实现比较方便呢?当然最容易想到的是对两个数组做循环,即写两个for循环来实现。这种写法大部分同学应该都会,而且也没有太多的技术含量,本博主就不解释了。这里给大家使用更为装bility的一些方法。老规矩,talkischeap,showmethecode#!/usr/bin/envpython#

    2022年6月21日
    26
  • VisiFire示例

    VisiFire示例Syntax:LabelText=”#AxisXLabel,#YValue” /> BelowisthechartaftersettingLabelTextproperty:   Modifiers: Modifier

    2022年7月21日
    12
  • Boost.Lockfree官方文档[通俗易懂]

    Boost.Lockfree官方文档[通俗易懂]目录介绍与动机简介与术语非阻塞数据结构的性质非阻塞数据结构的性能阻塞行为的来源数据结构数据结构配置示例队列栈无等待单生产者/单消费者队列脚注介绍与动机简介与术语术语“非阻塞”表示并发数据结构,该结构不使用传统的同步原语(例如警卫程序)来确保线程安全。MauriceHerlihy和NirShavit(比较“多处理器编程的艺术”)区分了3种类型的非阻塞数据结构,每种结构具有不同的属性:如果保证每个并发操作都可以在有限的步骤中完成,则数据.

    2022年7月19日
    25
  • idea 2021.9 激活码(JetBrains全家桶)

    (idea 2021.9 激活码)本文适用于JetBrains家族所有ide,包括IntelliJidea,phpstorm,webstorm,pycharm,datagrip等。IntelliJ2021最新激活注册码,破解教程可免费永久激活,亲测有效,下面是详细链接哦~https://javaforall.net/100143.html…

    2022年3月26日
    78
  • PHP rand()和mt_rand()的区别

    PHP rand()和mt_rand()的区别

    2021年10月18日
    41
  • Esp8266学习之旅① 搭建开发环境,开始一个“hellow world”串口打印。

    Esp8266学习之旅① 搭建开发环境,开始一个“hellow world”串口打印。本系列博客学习由非官方人员半颗心脏潜心所力所写,仅仅做个人技术交流分享,不做任何商业用途。如有不对之处,请留言,本人及时更改。1、Esp8266之搭建开发环境,开始一个“hellowworld”串口打印。2、Esp8266之利用GPIO开始使用按钮点亮你的“第一盏灯”。3、Esp8266之利用“软件定时器”定时0.5秒闪烁点亮一盏LED。4、Esp8266之了解P

    2022年5月30日
    54

发表回复

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

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