2011-09-01 66 views
1

鑑於webtest似乎沒有3.x版本(或任何開發一個的計劃),是否有解決方案來自動化系統測試WSGI應用程序?我知道單元測試的單元測試 - 我對整個系統測試的時刻更感興趣。Python 3.x WSGI測試框架

我不是在尋找工具來幫助開發一個應用 - 只需測試它。

+0

只是讓你知道情況是什麼。 webtest依賴於webob。目前webob上正在進行一些工作,使其能夠在python 3上運行。想象一下,當webob上的工作完成時,webtest應該只能在python 3上工作,或者應該儘可能少地工作。所以說「或者有什麼開發計劃」並不完全正確,但現在你知道情況了。 –

+0

這很好聽。可能是WebTest wiki的好信息。我發現的唯一一篇文章就是[這篇文章](http://groups.google.com/group/paste-users/browse_thread/thread/af25d39867d4cbe1),其中有人提到了Python 3支持,但從未收到回覆。 – threewestwinds

+0

這個問題現在已經過了幾年了,而且事情可能已經改變了。在沒有指出任何更改日誌或什麼的情況下,我只能說我正在使用'webtest'和一個Python 3.4應用程序(基於'Falcon'框架)。 – Dirk

回答

2

如果有其他人出現這種情況,我最終自己寫了一個解決方案。
下面是我使用的一個非常簡單的類 - 我只是從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 = []