Monkeypatching 内置插件

问题描述

为了测试,我想修补一些模块,特别是 osdatetime。我在猴子补丁 os 上成功了,但在 datetime 上没有成功:

TypeError: can't set attributes of built-in/extension type 'datetime.datetime'

我的设置是:

file_to_test.py

import os
import datetime

def os_function():
    return os.path.exists('some_path')

def datetime_function():
    return datetime.datetime.now()

test_file.py:

import file_to_test
import datetime

mockdate = datetime.datetime(2000,1,0)


def test_function_to_test(monkeypatch):
    monkeypatch.setattr(file_to_test.os.path,'exists',(lambda x: True))
    monkeypatch.setattr(file_to_test.datetime.datetime,'now',(lambda x: mockdate))
    file_exists = file_to_test.os_function()
    date = file_to_test.datetime_function()

    assert file_exists
    assert date == mockdate

谢谢!!

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)