I am new to mock and unit testing in python. How can I mock the local variable of a function? For example, how do I change age to 10 instead of 27 while testing?
# data_source.py
def get_name():
age = 27 #real value
return "Alice"
# person.py
from data_source import get_name
class Person(object):
def name(self):
return get_name()
# The unit test
from mock import patch
from person import Person
@patch('person.age')
def test_name(mock_age):
mock_age = 10 # mock value
person = Person()
name = person.name()
assert age == 10
agevalue from outside is required in order to test the function, then setting that value should not be a part of the function's work. Here, the test system is automatically warning you about a major code-design issue, simply by existing.