2011-09-01 8 views
1

WebTest가 3.x 버전 (또는 개발 계획)이없는 것 같으면 WSGI 애플리케이션의 자동화 된 시스템 테스트를위한 솔루션이 있습니까? 유닛 테스트를위한 unittest를 알고 있습니다 - 전체 시스템 테스트에서 그 순간에 더 관심이 있습니다. 단지 테스트을 -Python 3.x WSGI 테스팅 프레임 워크

나는 에게 응용 프로그램을 개발하는 데 도움이 도구를 찾는 게 아니에요.

+0

그래서 상황을 알 수 있습니다. webtest는 webob에 의존합니다. webob에서 파이썬 3에서 작동하게하는 작업이 지금 당장 있습니다. webob에 대한 작업이 끝나면 웹 테스트는 파이썬 3에서 작동하거나 최소한의 노력으로해야한다고 상상 하기엔 너무 스트레칭이 아닙니다. 따라서 "또는 어떤 것을 개발할 계획"이라고 말하는 것이 정확하지는 않지만 이제는 상황을 알고 있습니다. –

+0

듣기 좋습니다. WebTest 위키에 대한 좋은 정보 일 수 있습니다. 내가 부분적으로 관련성이있는 유일한 게시물은 [이 게시물] (http://groups.google.com/group/paste-users/browse_thread/thread/af25d39867d4cbe1)이었는데 다른 누군가가 파이썬 3 지원에 대해 언급했지만 응답을받지 못했습니다 . – threewestwinds

+0

이 질문은 현재 몇 년 전의 일이며 상황이 바뀌었을 수도 있습니다. 어떤 changelog 나 다른 것을 지적하지 않고, ('Falcon' 프레임 워크에 기반한) 파이썬 3.4 애플리케이션으로'webtest'를 성공적으로 사용하고 있다고 말할 수 있습니다. – Dirk

답변

2

다른 누구에게도이 문제가 발생하면 솔루션을 직접 작성하게되었습니다.
다음은 내가 사용하는 매우 단순한 클래스입니다 -대신에 WSGIBaseTest을 상속 받고 요청을 전달할 수있는 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 = [] 
관련 문제