我有以下测试正确日志格式的方法。
@patch('sys.stderr', new_callable=StringIO)
@mock.patch('socket.gethostname', return_value='testing')
def test_logging(self, gethostname_function, mock_stderr):
logger = logging.getLogger('project.logging')
app_logging.init_logging()
logger.info('testing mesage')
assert mock_stderr.getvalue() == '{"message": "testing mesage", "levelname": "INFO", "process": 37284, "asctime": "2018-03-01 13:23:33,968", "hostname": "testing"}\n'
格式化程序如下所示:
(message) (levelname) (process) (asctime)
如何模拟日期时间和流程ID?感谢
答案 0 :(得分:1)
确定,
@patch('time.time', mock_time)
@patch('os.getpid', mock_os_pid)
是解决方案。对不起,浪费你的时间:))