我是 python 中的模拟和单元测试的新手。如何模拟函数的局部变量?例如,如何age
在测试时更改为 10 而不是 27?
# 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