I am fairly new to python. I am trying to use mock to write a unit test. 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.
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)
gmuraleekrishna
3,4311 gold badge29 silver badges46 bronze badges
-
1Possible duplicate of mocking functions using python mockgmuraleekrishna– gmuraleekrishna2017年01月06日 06:38:58 +00:00Commented Jan 6, 2017 at 6:38
-
Thanks! but I don't think it's a duplicate of mocking functions using python mockbarmd– barmd2017年01月06日 10:11:10 +00:00Commented Jan 6, 2017 at 10:11
1 Answer 1
# 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. Do you want this?
answered Jan 6, 2017 at 5:59
lxyscls
3291 gold badge3 silver badges20 bronze badges
Sign up to request clarification or add additional context in comments.
1 Comment
barmd
In line o = cl.func(v), it would be cl().func(v)
lang-py