2016-12-12 4 views
5

के लिए अनुरोध-जैसे रैपर, मैं अपने पैकेज के लिए उपयोग करने योग्य परीक्षण करने की कोशिश कर रहा हूं, लेकिन Flask.test_client का उपयोग requests एपीआई से बहुत अलग है जिसे मुझे उपयोग करना मुश्किल लगता है।फ्लास्क के test_client

मैं requests.adapters.HTTPAdapter प्रतिक्रिया लपेटो करने की कोशिश की है, लेकिन यह werkzeug तरह लग रहा है (उस बात के लिए या urllib) httplib उपयोग नहीं करता है यह खुद Response वस्तु का निर्माण करने के लिए।

कोई विचार यह कैसे किया जा सकता है? मौजूदा कोड का संदर्भ सबसे अच्छा होगा (googling werkzeug + अनुरोध कोई उपयोगी परिणाम नहीं देता है)

बहुत धन्यवाद !!

उत्तर

0

जैसा कि मैंने समझ लिया कि आपको क्या चाहिए मॉकिंग (What is Mocking? और Mock Object देखें)। खैर, अपने विकल्पों की जोड़ी नीचे सूचीबद्ध:

  • आपका उपहास करने के लिए जा रहे हैं, तो केवल पैकेज उपयोग requests-mock library जो अनुरोध पैकेज से जवाब बाहर नकली करने का इरादा अनुरोध करता है।

  • एक अन्य विकल्प अजगर mock libary का उपयोग requests मॉड्यूल उपहास करने के लिए, यह भी An Introduction to Mocking in Python को देखने के लिए है।

+0

धन्यवाद में वर्णित है, लेकिन मैं एडाप्टर चाहता हूं, नकली नहीं। एक मिडलवेयर बनाना जो 'flask.test_client' से' request_mock' का अनुवाद करता है, केवल 'अनुरोधों को डाउनलोड करने के ऊपर ओवरहेड लगता है। एडाप्टर' – eplaut

0

आप requests विधि नीचे कोड अनुभाग में परिभाषित कर सकते हैं। इसका उपयोग करने के लिए आपको नीचे दिए गए कोड में दिखाए गए get_auth_headers विधि को भी परिभाषित करने की आवश्यकता होगी।

विशेषताएं:

  • बोतल परीक्षण ग्राहक के आसपास आवरण।
  • स्वचालित रूप से अनुरोध शीर्षलेख सेट करें।

    • Content-Type: Application/json जब json
    • Basic Authentication जब auth गुजर निर्माण गुजर।
  • फ्लास्क परीक्षण क्लाइंट के लिए स्ट्रिंग के रूप में आपके json डेटा को डंप करता है।
  • (गुम) एक requests.Response वस्तु

कोड में प्रतिक्रिया लपेटें:

class MyTestCase(unittest.TestCase): 
    def setUp(self): 
     self.app = create_app('testing') 
     self.client = self.app.test_client() 
     self.app_context = self.app.app_context() 
     self.app_context.push() 
     db.create_all() 

    def tearDown(self): 
     db.session.remove() 
     db.drop_all() 
     self.app_context.pop() 

    def get_auth_headers(self, username, password): 
     return { 
      'Authorization': 
       'Basic ' + base64.b64encode(
        (username + ':' + password).encode('utf-8')).decode('utf-8'), 
      'Accept': 'application/json', 
      'Content-Type': 'application/json' 
     } 

    def requests(self, method, url, json={}, auth=(), **kwargs): 
     """Wrapper around Flask test client to automatically set headers if JSON 
     data is passed + dump JSON data as string.""" 
     if not hasattr(self.client, method): 
      print("Method %s not supported" % method) 
      return 
     fun = getattr(self.client, method) 

     # Set headers 
     headers = {} 
     if auth: 
      username, password = auth 
      headers = self.get_auth_headers(username, password) 

     # Prepare JSON if needed 
     if json: 
      import json as _json 
      headers.update({'Content-Type': 'application/json'}) 
      response = fun(url, 
          data=_json.dumps(json), 
          headers=headers, 
          **kwargs) 
     else: 
      response = fun(url, **kwargs) 
     self.assertTrue(response.headers['Content-Type'] == 'application/json') 
     return response 

उपयोग (एक परीक्षण मामले में):

def test_requests(self): 
    url = 'http://localhost:5001' 
    response = self.requests('get', url) 
    response = self.requests('post', url, json={'a': 1, 'b': 2}, auth=('username', 'password')) 
    ... 

फर्क सिर्फ इतना है requests के साथ टाइपिंग के बजाय है requests.get(...) आपको self.request('get', ...) टाइप करना होगा।

तुम सच में requests व्यवहार चाहते हैं, आप के चारों ओर अपने स्वयं के get, put, post, delete रैपर निर्धारित करने होंगे requests

नोट: इस के लिए हो सकता है ऐसा करने के लिए

सबसे अनुकूल तरीके उपclass FlaskClient जैसा कि Flask API documentation