Python 3.x WSGI测试框架

2 投票
1 回答
563 浏览
提问于 2025-04-17 01:02

因为webtest似乎没有3.x版本(也没有计划开发一个),那么有没有什么方法可以自动测试WSGI应用程序呢?我知道unittest是用来做单元测试的,但我现在更关心的是整个系统的测试。

我并不是在寻找帮助我开发应用程序的工具,而是想要一些可以用来测试它的工具。

1 个回答

4

如果还有其他人遇到这个问题,我自己写了一个解决方案。
这是我使用的一个非常简单的类——我直接从 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 = []

撰写回答