"""
Created on 10/10/17 11:27 AM
@author: Chen Liang
@function: 日志模块 单元测试
"""
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
import unittest
import mock
import datetime
from qk_log import log_init, dlog, ilog, wlog, elog
class TestQkLog(unittest.TestCase):
dt_str = datetime.datetime.strptime('2017-10-11 11:08:59', '%Y-%m-%d %H:%M:%S')
@mock.patch('qk_log.os.path')
@mock.patch('qk_log.datetime.datetime')
@mock.patch('qk_log.logging')
@mock.patch('qk_log.find_caler')
def test_dlog(self, mock_caler, mock_logging, mock_datetime, mock_path):
mock_path.exists.return_value = True
log_init()
self.assertFalse(mock_logging.getLogger('debug').debug.called, "Failed to not write log.")
mock_caler.return_value = ('qk_log_test', 12, '')
mock_datetime.now.return_value = self.dt_str
dlog('any msg')
mock_logging.getLogger('debug').debug.assert_called_with(
'[%s] [%s] [%s,%d] %s' % ('2017-10-11 11:08:59', 'debug', 'qk_log_test', 12, 'any msg'))
@mock.patch('qk_log.os.path')
@mock.patch('qk_log.datetime.datetime')
@mock.patch('qk_log.logging')
@mock.patch('qk_log.find_caler')
def test_ilog(self, mock_caler, mock_logging, mock_datetime, mock_path):
mock_path.exists.return_value = True
log_init()
self.assertFalse(mock_logging.getLogger('debug').info.called, "Failed to not write log.")
mock_caler.return_value = ('qk_log_test', 12, '')
mock_datetime.now.return_value = self.dt_str
ilog('any msg')
mock_logging.getLogger('debug').info.assert_called_with(
'[%s] [%s] [%s,%d] %s' % ('2017-10-11 11:08:59', 'info', 'qk_log_test', 12, 'any msg'))
@mock.patch('qk_log.os.path')
@mock.patch('qk_log.datetime.datetime')
@mock.patch('qk_log.logging')
@mock.patch('qk_log.find_caler')
def test_wlog(self, mock_caler, mock_logging, mock_datetime, mock_path):
mock_path.exists.return_value = True
log_init()
self.assertFalse(mock_logging.getLogger('warn').info.called, "Failed to not write log.")
mock_caler.return_value = ('qk_log_test', 12, '')
mock_datetime.now.return_value = self.dt_str
wlog('any msg')
mock_logging.getLogger('warn').warn.assert_called_with(
'[%s] [%s] [%s,%d] %s' % ('2017-10-11 11:08:59', 'warning', 'qk_log_test', 12, 'any msg'))
@mock.patch('qk_log.os.path')
@mock.patch('qk_log.datetime.datetime')
@mock.patch('qk_log.logging')
@mock.patch('qk_log.find_caler')
def test_elog(self, mock_caler, mock_logging, mock_datetime, mock_path):
mock_path.exists.return_value = True
log_init()
self.assertFalse(mock_logging.getLogger('error').info.called, "Failed to not write log.")
mock_caler.return_value = ('qk_log_test', 12, '')
mock_datetime.now.return_value = self.dt_str
elog('any msg')
mock_logging.getLogger('error').error.assert_called_with(
'[%s] [%s] [%s,%d] %s' % ('2017-10-11 11:08:59', 'error', 'qk_log_test', 12, 'any msg'))
if __name__ == '__main__':
unittest.main()