2013-01-10 14 views
10

मुझे हाल ही में हमारे बाकी एपीआई को फिर से लिखना पड़ा, और फ्लास्क से चेरीपी (ज्यादातर पायथन 3 संगतता के कारण) से स्विच किया गया। लेकिन अब मैं अपने यूनिट परीक्षण लिखने की कोशिश कर रहा हूं, फ्लास्क में वास्तव में निफ्टी निर्मित परीक्षण क्लाइंट है, जिसका उपयोग आप अपने आवेदन में नकली अनुरोध भेजने के लिए कर सकते हैं (सर्वर शुरू किए बिना।) मुझे कोई समान कार्यक्षमता नहीं मिल रही है चेरीपी के लिए, क्या ऐसी कार्यक्षमता है, या क्या मैं सर्वर शुरू करना और इसके खिलाफ वास्तविक अनुरोध कर रहा हूं?चेरीपी वेबपैप को अनइस्टेस्टिंग

उत्तर

18

जहाँ तक मुझे पता है, चेरीपी वास्तव में इस प्रकार के परीक्षण (कोई रनिंग सर्वर) के लिए सुविधा प्रदान नहीं करता है। लेकिन फिर भी यह करना काफी आसान है (हालांकि यह चेरीपी के कुछ आंतरिक पर निर्भर करता है)।

from StringIO import StringIO 
import unittest 
import urllib 

import cherrypy 

local = cherrypy.lib.httputil.Host('127.0.0.1', 50000, "") 
remote = cherrypy.lib.httputil.Host('127.0.0.1', 50001, "") 

class Root(object): 
    @cherrypy.expose 
    def index(self): 
     return "hello world" 

    @cherrypy.expose 
    def echo(self, msg): 
     return msg 

def setUpModule(): 
    cherrypy.config.update({'environment': "test_suite"}) 

    # prevent the HTTP server from ever starting 
    cherrypy.server.unsubscribe() 

    cherrypy.tree.mount(Root(), '/') 
    cherrypy.engine.start() 
setup_module = setUpModule 

def tearDownModule(): 
    cherrypy.engine.exit() 
teardown_module = tearDownModule 

class BaseCherryPyTestCase(unittest.TestCase): 
    def webapp_request(self, path='/', method='GET', **kwargs): 
     headers = [('Host', '127.0.0.1')] 
     qs = fd = None 

     if method in ['POST', 'PUT']: 
      qs = urllib.urlencode(kwargs) 
      headers.append(('content-type', 'application/x-www-form-urlencoded')) 
      headers.append(('content-length', '%d' % len(qs))) 
      fd = StringIO(qs) 
      qs = None 
     elif kwargs: 
      qs = urllib.urlencode(kwargs) 

     # Get our application and run the request against it 
     app = cherrypy.tree.apps[''] 
     # Let's fake the local and remote addresses 
     # Let's also use a non-secure scheme: 'http' 
     request, response = app.get_serving(local, remote, 'http', 'HTTP/1.1') 
     try: 
      response = request.run(method, path, qs, 'HTTP/1.1', headers, fd) 
     finally: 
      if fd: 
       fd.close() 
       fd = None 

     if response.output_status.startswith('500'): 
      print response.body 
      raise AssertionError("Unexpected error") 

     # collapse the response into a bytestring 
     response.collapse_body() 
     return response 

class TestCherryPyApp(BaseCherryPyTestCase): 
    def test_index(self): 
     response = self.webapp_request('/') 
     self.assertEqual(response.output_status, '200 OK') 
     # response body is wrapped into a list internally by CherryPy 
     self.assertEqual(response.body, ['hello world']) 

    def test_echo(self): 
     response = self.webapp_request('/echo', msg="hey there") 
     self.assertEqual(response.output_status, '200 OK') 
     self.assertEqual(response.body, ["hey there"]) 

     response = self.webapp_request('/echo', method='POST', msg="hey there") 
     self.assertEqual(response.output_status, '200 OK') 
     self.assertEqual(response.body, ["hey there"]) 

if __name__ == '__main__': 
    unittest.main() 

संपादित करें, मैं एक CherryPy recipe के रूप में इस उत्तर का विस्तार किया है:

यहाँ एक साधारण सा प्रदर्शन है।

+0

धन्यवाद, आज इस एक कोशिश देने जा रहा। – Blubber

+0

एक आकर्षण की तरह काम करता है, धन्यवाद फिर से :)। – Blubber

+0

@Sylvain: 'cherrypy.tools.json_in() 'समर्थन के लिए कुछ विशेष आवश्यक है? testcases में 'cherrypy.request.json'' कारण बनता है 'विशेषता': 'अनुरोध' ऑब्जेक्ट में कोई विशेषता नहीं है 'json'' – user1338062

2

ऐसा लगता है कि एकजुट करने का एक वैकल्पिक तरीका है। मैंने अभी पाया और निम्नलिखित नुस्खा की जांच की जो चेरीपी 3.5 के साथ ठीक काम करता है।

http://docs.cherrypy.org/en/latest/advanced.html#testing-your-application

import cherrypy 

    from cherrypy.test import helper 

    class SimpleCPTest(helper.CPWebCase): 
     def setup_server(): 
      class Root(object): 
       @cherrypy.expose 
       def echo(self, message): 
        return message 

      cherrypy.tree.mount(Root()) 
     setup_server = staticmethod(setup_server) 

     def test_message_should_be_returned_as_is(self): 
      self.getPage("/echo?message=Hello%20world") 
      self.assertStatus('200 OK') 
      self.assertHeader('Content-Type', 'text/html;charset=utf-8') 
      self.assertBody('Hello world') 

     def test_non_utf8_message_will_fail(self): 
      """ 
      CherryPy defaults to decode the query-string 
      using UTF-8, trying to send a query-string with 
      a different encoding will raise a 404 since 
      it considers it's a different URL. 
      """ 
      self.getPage("/echo?message=A+bient%F4t", 
         headers=[ 
          ('Accept-Charset', 'ISO-8859-1,utf-8'), 
          ('Content-Type', 'text/html;charset=ISO-8859-1') 
         ] 
      ) 
      self.assertStatus('404 Not Found') 
+0

यह निश्चित रूप से चेरीपी परीक्षणों को आंतरिक रूप से कैसे लिखा जाता है। लेकिन इसका मतलब है कि एक वास्तविक सर्वर शुरू करना। यह स्वचालित है लेकिन मेरी नुस्खा दिखाने की कोशिश की है कि आप बिना किसी रनिंग सर्वर के अपने हैंडलर का परीक्षण कर सकते हैं। आपकी प्रतिक्रिया एकीकरण पक्ष पर थोड़ा और गलत नहीं है जो मैं कहूंगा। –