[英]mocking global variables on python doesn't work
I am trying to mock global variable using mock engine, but it seems that it doesn't simply work for my variables. 我试图使用模拟引擎模拟全局变量,但似乎它不仅仅适用于我的变量。 When I patch for example os.name
it works perfectly fine, however for my custom variables it doesn't work. 当我修补例如os.name
它完全正常,但是对于我的自定义变量它不起作用。 Here is the code: 这是代码:
global_var.py global_var.py
var = 10
use_global_var.py use_global_var.py
from global_var import var
def test_call():
return var
test.py test.py
import mock
from use_global_var import test_call
@mock.patch('global_var.var', 50)
def test_check():
print(test_call())
test_check()
print
is supposed to return 50 if I understand it right, but it returns 10. Does anybody know what is the problem here and how to solve it? 如果我理解正确, print
应该返回50,但它返回10.有人知道这里有什么问题以及如何解决它?
You aren't mocking the right name . 你没有嘲笑正确的名字 。 use_global_var.test_call
is looking at the name use_global_var.var
, but you are mocking global_var.var
. use_global_var.test_call
正在查看名称use_global_var.var
,但您正在global_var.var
。
@mock.patch('use_global_var.var', 50)
def test_check():
print(test_call())
test_check()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.