apps.my_module.py
JavaScript
x
8
1
def my_func(name: str) -> str:
2
if name == 'a':
3
return 'name is a'
4
elif name == 'b':
5
return 'name is b'
6
else:
7
return 'not defined'
8
I want to mock my_func
in tests.py
the mocked function return value for a
is name is AA
not name is a
, but the return value for b
and else must stay the same
I want to do this using unittest
, how to do this?
Advertisement
Answer
The solution goes as the following:
in tests.py
JavaScript
1
12
12
1
from apps import my_module
2
import unitteest
3
4
class test(unittest.TestCase):
5
def any_test(self, *_):
6
original_func = my_module.my_func
7
def mocked_func(name):
8
if name == 'a':
9
return 'name is AA'
10
else:
11
return original_func(name)
12