के साथ बुलाया गया है। मैं एक सरल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)
एक आकर्षण की तरह काम करता है, बहुत बहुत शुक्रिया उपयोग कर सकते हैं! –
चूंकि इसे पायथन 2.7 के लिए टैग किया गया है, इसलिए एक पूर्ण उत्तर को https://github.com/testing-cabal/mock पर बैकपोर्ट का उल्लेख करना चाहिए, जबकि यह इसके अंतर्निहित मॉड्यूल का हिस्सा नहीं है। –