Python 3.x WSGI 测试框架

发布于 2024-12-02 10:17:30 字数 159 浏览 2 评论 0原文

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

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

Given that webtest doesn't seem to have a 3.x version (or any plans to develop one), are there any solutions for automated system testing of a WSGI application? I know unittest for unit testing - I'm more interested in the moment in whole systems tests.

I'm not looking for tools to help develop an application - just test it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

她说她爱他 2024-12-09 10:17:30

如果其他人遇到这个问题,我最终自己编写了一个解决方案。
这是我使用的一个非常简单的类 - 我只是继承自 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 = []

In case anyone else comes upon this, I ended up writing a solution myself.
Here's a very simple class I use - I just inherit from WSGIBaseTest instead of TestCase, and get a method self.request() that I can pass requests into.
It stores cookies, and will automatically send them into the application on later requests (until self.new_session() is called).

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 = []
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文