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)
上一篇 2022年1月30日 下午1:00
下一篇 2022年1月30日 下午2:00


相关推荐

  • Linux常用打包压缩命令

    Linux常用打包压缩命令简介Linux上常用的压缩/解压工具,介绍了zip、rar、tar的使用。文件打包和压缩Linux上的压缩包文件格式,除了Windows最常见的*.zip、*.rar、.7z后缀的压缩文件,还有.gz、.xz、.bz2、.tar、.tar.gz、.tar.xz、tar.bz2文件后缀名说明*.zipzip程序打包压缩的文件*.rarrar程序压…

    2022年5月6日
    40
  • KVM+显卡直通(passthrough)配置方法及问题说明

    KVM+显卡直通(passthrough)配置方法及问题说明1 硬件条件首先要确定主板和 CPU 都支持 VT d 技术 即 Virtualizati O 英特尔虚拟技术 近年的产品应该都支持此技术 在 BIOS 里将还要确定要直通的显卡支持 PCIPass through 似乎 A 卡对于直通的支持比 N 卡好 但 N 卡性能比 A 卡好 这个大家都知道 目前市面上的显卡一般都支持直通 我用过的 NVIDIA 的 M60 和 GeF

    2026年3月19日
    1
  • python2 nonlocal_python关键字及用法

    python2 nonlocal_python关键字及用法python变量引用顺序:从当前作用域开始寻找变量,如果没找到就往上一层作用域寻找,没找到就再上一层……即:当前作用域局部变量->外层作用域变量->再外层作用域变量->……->当前模块全局变量->pyhton内置变量global:全局变量nonlocal:外层嵌套函数的变量使用总结:局部作用域改变全局变量用global,global同时还可以定义新的…

    2025年9月21日
    8
  • idea怎么搭建springboot_你没有创建该项目的权限

    idea怎么搭建springboot_你没有创建该项目的权限一般来说,用IDEA创建SpringBoot项目有两种方式。其一是Spring官网提供了一个快速生成SpringBoot项目的网站,可以在官网直接下载后,再导入IDEA中。另外一种是直接用IDEA创建一个SpringBoot项目,一般开发也是用的这种方式进行创建。虽说SpringBoot简化了Spring的配置,但学习之前需要对Spring基础知识有一定的掌握。……

    2022年10月13日
    6
  • 怎么用sql脚本创建数据库_mysql数据库导入

    怎么用sql脚本创建数据库_mysql数据库导入使用sql脚本建立数据库,可以方便各用户,各数据库之间的复制使用,下面将在cmd中完成上述操作:cmd中mysql基本操作:1.连结mysql:C:\Users\WJ>mysql-h127.0.0.1-uroot-p123456其中-h表示host127.0.0.1表示地址,这里你如果是远程访问的话,直接写上远程地址即可,-u-p分别为用户名及密码;2.查看所有数据库:showdatabases;3.操作某一数据库:useschool_2;4.查看该数据库下的表:s

    2026年3月4日
    5
  • 搭建python开发平台「建议收藏」

    搭建python开发平台「建议收藏」python开发

    2022年7月3日
    28

发表回复

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

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