我想用
unittest
测试我制作的SimpleXMLRPCServer的一些功能。与Mock一起,我现在试图断言,当到达if语句时,已记录了特定消息,但我无法使其工作。我尝试过在StackOverflow或谷歌搜索中找到各种答案,但仍然没有成功。我在测试用例中进行的调用如下:
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))))
服务器中的功能如下:
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))))
知道为什么不起作用吗?我是Python单元测试的新手,断言一个记录器已经做了一些事情,这似乎是我可能面临的最大问题,所以我可能在代码中搞砸了一些非常简单的事情。我们将非常感谢您的任何帮助!
编辑:为了完整,这里是测试输出和失败:
.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)