1

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
asked Jan 6, 2017 at 5:27
2

1 Answer 1

0
# 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
Sign up to request clarification or add additional context in comments.

1 Comment

In line o = cl.func(v), it would be cl().func(v)

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.