pytest - 模拟过程和时间

问题描述 投票:0回答:1

我有以下方法来测试正确的日志格式。

@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?谢谢

python mocking pytest
1个回答
1
投票

好,

@patch('time.time', mock_time)
@patch('os.getpid', mock_os_pid)

是解决方案。对不起,浪费你的时间:)

© www.soinside.com 2019 - 2024. All rights reserved.