2015-07-30 9 views
7

के साथ बुलाया गया है। मैं एक सरलXMLRPCServer के कुछ कार्यों का परीक्षण करने के लिए unittest का उपयोग करने का प्रयास कर रहा हूं। मॉक के साथ टोगेथेर, अब मैं यह कहने की कोशिश कर रहा हूं कि यदि कोई कथन पूरा हो गया है तो एक विशिष्ट संदेश लॉग किया गया है, लेकिन मैं इसे काम नहीं कर सकता। मैंने StackOverflow या Googling द्वारा यहां मिले विभिन्न उत्तरों को लागू करने का प्रयास किया है, लेकिन अभी भी कोई भाग्य नहीं है। कॉल मैं टेस्ट के मामले में बनाने के इस प्रकार हैं:जोर दें कि लॉगिंग को विशिष्ट स्ट्रिंग

def listen_for_tasks(self, release, component): 
    item = {'release': release, 'component': component} 
    for el in list(self._queue.queue): 
     if self.is_request_duplicate(el, item): 
      logger.debug('Already have a request' 
         ' for this component: {}'.format(item)) 
      return 
    self._queue.put(item, False) 
    if len(self._queue.queue) > 50: 
     logger.warning('There are currently {}' 
         ' items in the queue'.format(
         str(len(self._queue.queue)))) 

किसी भी विचार क्यों यह काम नहीं कर रहा:

def test_listen_for_tasks(self): 
    el = {'release': 'default', 'component': None} 
    for i in range(50): 
     self.server._queue.put(el) 
    ServerThread.listen_for_tasks(self.server, 'bla', 'blabla') 
    with mock.patch('queue_server.logging') as mock_logging: 
     mock_logging.warning.assert_called_with('There are currently {}' 
               ' items in the queue'.format(
               str(len(self.server._queue.queue)))) 

सर्वर में समारोह इस प्रकार है? मैं पाइथन में यूनिट परीक्षण के लिए नया हूं और जोर दे रहा हूं कि एक लॉगर ने कुछ ऐसा किया है जो किसी को सामना कर सकता है, इसलिए मैं कोड में वास्तव में कुछ आसान हो सकता था। किसी भी तरह की मदद की सराहना की जाएगी!

संपादित करें: पूर्णता के लिए, यहाँ है परीक्षण उत्पादन और विफलता:

.No handlers could be found for logger "queue_server" 
F 


FAIL: test_listen_for_tasks (__main__.TestQueueServer) 

Traceback (most recent call last): 
    File "artifacts_generator/test_queue_server.py", line 46, in test_listen_for_tasks 
str(len(self.server._queue.queue)))) 
    File "/home/lugiorgi/Desktop/Code/publisher/env/local/lib/python2.7/site-packages/mock/mock.py", line 925, in assert_called_with 
raise AssertionError('Expected call: %s\nNot called' % (expected,)) 
AssertionError: Expected call: warning('There are currently 51 items in the queue') 
Not called 

Ran 2 tests in 0.137s 

FAILED (failures=1) 

उत्तर

7

आप पहले नकली वस्तु की जरूरत है, तो कॉल समारोह आप परीक्षण करना चाहते।

ऑब्जेक्ट का मज़ाक उड़ाते समय, आपको उस ऑब्जेक्ट का पूर्ण पैकेज और ऑब्जेक्ट/फ़ंक्शन नाम भी प्रदान करना होगा, जिसे आप मॉकिंग कर रहे हैं, एक परिवर्तनीय नाम नहीं।

अंत में, patch के सजावटी रूप का उपयोग करना अक्सर अधिक सुविधाजनक होता है।

तो, उदाहरण के लिए:

logger = logging.getLogger(__name__) 

def my_fancy_function(): 
    logger.warning('test') 

@patch('logging.Logger.warning') 
def test_my_fancy_function(mock): 
    my_fancy_function() 
    mock.assert_called_with('test') 

# if you insist on using with: 
def test_my_fancy_function_with_with(): 
    with patch('logging.Logger.warning') as mock: 
     my_fancy_function() 
     mock.assert_called_with('test') 
+0

एक आकर्षण की तरह काम करता है, बहुत बहुत शुक्रिया उपयोग कर सकते हैं! –

+0

चूंकि इसे पायथन 2.7 के लिए टैग किया गया है, इसलिए एक पूर्ण उत्तर को https://github.com/testing-cabal/mock पर बैकपोर्ट का उल्लेख करना चाहिए, जबकि यह इसके अंतर्निहित मॉड्यूल का हिस्सा नहीं है। –

2

अजगर 3.4 जब से तुम unittest.TestCase वर्ग विधि assertLogs

import logging 
import unittest 


class LoggingTestCase(unittest.TestCase): 
    def test_logging(self): 
     with self.assertLogs(level='INFO') as log: 
      logging.info('Log message') 
      self.assertEqual(len(log.output), 1) 
      self.assertEqual(len(log.records), 1) 
      self.assertIn('Log message', log.output[0]) 
+0

धन्यवाद, लॉगर्स से जुड़े परीक्षण मामलों को संबोधित करने का यह सबसे अच्छा तरीका है। – Abhijeet

संबंधित मुद्दे