Python修补动态调用的函数

2024-09-27 17:48:57 发布

您现在位置:Python中文网/ 问答频道 /正文

我想修补作业模板标签,但这个问题不是Django特有的。

以下是我想要达到的目标:

@patch('the_caller_path.my_function', _my_mock)
class MyTests(...):

我得到的“明显”错误是:

AttributeError: <module 'the_caller_path' from '...'> does not have the attribute 'my_function'

问题是the_caller动态调用my_function。你知道吗


正如预期的那样,使用路径WARE声明函数没有任何效果:

@patch('the_real_path.my_function', _my_mock)
class MyTests(...):

my_function未修补。你知道吗


如何对函数进行全局修补?你知道吗


Tags: thepathdjango函数模板目标my作业

热门问题