[英]mocking python class dictionary
I am fairly new to python. 我对python很新。 I am trying to use mock to write a unit test. 我正在尝试使用mock来编写单元测试。 Here is the pattern of the code. 这是代码的模式。
# mod3.py
import mod1.class1
import mod2.class2
d = {
"c1": class1
"c2": class2
}
def func1(c, v):
cl = d[c]
o = cl().meth1(v)
return o
I want to write an unit test for func1. 我想为func1编写一个单元测试。
def test_func1(c, v):
c, v = mock.Mock(), mock.Mock()
r = mod3.func1(c,v)
e = {"key1": "value1"}
#want to check if the ret val is as expected
How would I use mock to essentially mock cl().meth1(v)
我如何使用mock来实际模拟cl().meth1(v)
# cat class1.py
def func(v):
return v
# cat class2.py
def func(v):
return v
#cat mock.py
import class1, class2
d = { "c1": class1, "c2": class2 }
def func1(c, v):
cl = d[c]
o = cl.func(v)
return o
class1 and class2 are modules in python. class1和class2是python中的模块。 Do you want this? 你想要这个吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.