【问题标题】:Python 3.x WSGI Testing frameworkPython 3.x WSGI 测试框架
【发布时间】:2011-09-01 06:36:14
【问题描述】:

鉴于 webtest 似乎没有 3.x 版本(或任何开发的计划),是否有针对 WSGI 应用程序的自动化系统测试的解决方案?我知道 unittest 用于单元测试 - 我对整个系统测试的时刻更感兴趣。

我不是在寻找帮助开发应用程序的工具——只是测试它。

【问题讨论】:

  • 只是让您知道情况如何。 webtest 依赖于 webob。现在 webob 上正在做一些工作,以使其在 python 3 上工作。想象当 webob 上的工作完成后,webtest 应该只在 python 3 上工作或者应该以最小的努力工作,这并不难。所以说“或任何开发计划”并不完全正确,但现在你知道情况了。
  • 很高兴听到这个消息。可能是 WebTest wiki 的好信息。我发现的唯一一篇甚至部分相关的帖子是this post,其中有人提到了 Python 3 支持但从未收到回复。
  • 这个问题已经有几年了,同时事情可能已经发生了变化。在不指出任何更改日志或其他内容的情况下,我可以说我在 Python 3.4 应用程序中成功使用了webtest(基于Falcon 框架)。

标签: python testing python-3.x integration-testing wsgi


【解决方案1】:

万一其他人遇到这个问题,我最终自己编写了一个解决方案。
这是我使用的一个非常简单的类——我只是从WSGIBaseTest 继承而不是TestCase,并获得一个可以将请求传递到的方法self.request()
它存储cookies,并在以后的请求中自动将它们发送到应用程序中(直到调用self.new_session())。

import unittest
from wsgiref import util
import io

class WSGIBaseTest(unittest.TestCase):
    '''Base class for unit-tests. Provides up a simple interface to make requests
    as though they came through a wsgi interface from a user.'''
    def setUp(self):
        '''Set up a fresh testing environment before each test.'''
        self.cookies = []
    def request(self, application, url, post_data = None):
        '''Hand a request to the application as if sent by a client.
        @param application: The callable wsgi application to test.
        @param url: The URL to make the request against.
        @param post_data: A string.'''
        self.response_started = False
        temp = io.StringIO(post)
        environ = {
            'PATH_INFO': url,
            'REQUEST_METHOD': 'POST' if post_data else 'GET',
            'CONTENT_LENGTH': len(post),
            'wsgi.input': temp,
            }
        util.setup_testing_defaults(environ)
        if self.cookies:
            environ['HTTP_COOKIE'] = ';'.join(self.cookies)
        self.response = ''
        for ret in application(environ, self._start_response):
            assert self.response_started
            self.response += str(ret)
        temp.close()
        return response
    def _start_response(self, status, headers):
        '''A callback passed into the application, to simulate a wsgi
        environment.

        @param status: The response status of the application ("200", "404", etc)
        @param headers: Any headers to begin the response with.
        '''
        assert not self.response_started
        self.response_started = True
        self.status = status
        self.headers = headers
        for header in headers:
            # Parse out any cookies and save them to send with later requests.
            if header[0] == 'Set-Cookie':
                var = header[1].split(';', 1)
                if len(var) > 1 and var[1][0:9] == ' Max-Age=':
                    if int(var[1][9:]) > 0:
                        # An approximation, since our cookies never expire unless
                        # explicitly deleted (by setting Max-Age=0).
                        self.cookies.append(var[0])
                    else:
                        index = self.cookies.index(var[0])
                        self.cookies.pop(index)
    def new_session(self):
        '''Start a new session (or pretend to be a different user) by deleting
        all current cookies.'''
        self.cookies = []

【讨论】:

    猜你喜欢
    • 2011-06-12
    • 1970-01-01
    • 2012-05-22
    • 2012-05-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-10
    • 1970-01-01
    相关资源
    最近更新 更多