元芳怎么了
在 python 中没有“开箱即用”的解决方案,尽管我坚信在开发更高级的测试时能够模拟和检查局部变量发生的情况非常重要。我已经用一个新类来模拟一个函数并获取本地值,我希望它对你有所帮助。import inspectfrom textwrap import dedentimport reclass MockFunction: """ Defines a Mock for functions to explore the details on their execution. """ def __init__(self, func): self.func = func def __call__(mock_instance, *args, **kwargs): # Add locals() to function's return code = re.sub('[\\s]return\\b', ' return locals(), ', dedent( inspect.getsource(mock_instance.func))) code = code + f'\nloc, ret = {mock_instance.func.__name__}(*args, **kwargs)' loc = {'args': args, 'kwargs': kwargs} exec(code, mock_instance.func.__globals__, loc) # Put execution locals into mock instance for l,v in loc['loc'].items(): setattr(mock_instance, l, v) return loc['ret']要使用它:import unittestfrom unittest import mock# This is the function you would like to test. It can be defined somewhere elsedef foo(param_a, param_b=10): param_a = f'Hey {param_a}' # Local only param_b += 20 # Local only return 'bar'# Define a test to validate what happens to local variables when you call that functionclass SimpleTest(unittest.TestCase): @mock.patch(f'{__name__}.foo', autospec=True, side_effect=MockFunction(foo)) def test_foo_return_and_local_params_values(self, mocked): ret = foo('A') self.assertEqual('Hey A', mocked.side_effect.param_a) self.assertEqual(30, mocked.side_effect.param_b) self.assertEqual('bar', ret)正如我们所见,您可以使用模拟函数中的 side_effect 检查局部变量发生了什么。