python - 如何猴子补丁 datetime.datetime.now()?

标签 python monkeypatching

测试.py

@patch('I want to patch datetime.datetime.now here')
def test_function():
    import my_module
    my_module.some_function()
    ...

my_module.py

from datetime import datetime

def some_function():
    now = datetime.now()

顺便说一句,我正在使用 pytest。

最佳答案

我认为freezegun可能是您正在寻找的模块。

关于python - 如何猴子补丁 datetime.datetime.now()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34706778/

相关文章:

python - pytest-monkeypatch 装饰器(不使用模拟/补丁)

python - 是否可以向 Python 列表实例添加方法?

python - 如何在 python 中使用 np.pad 填充图像?

python - 递归搜索

python - 如何制作从1开始的N个连续奇数的列表

python-3.x - Monkeypatch setenv 值在 python unittest 中跨测试用例持续存在

python - Django Rest Framework Json 数据猴子修补

python-3.x - Pytest - 如何模拟或修补全局变量

python - 在python中解析大型伪xml文件

python - 使用 Python 进行 SOAP MTOM 调用