不确定这是否可行,但有没有办法模拟 eval 导入的函数?例如:一个.pyimport bdef code(): me = 'ME' should_be_changed = eval('b.mock' + me + '()') return should_be_changedb.pydef mockME(): return 'Dummy code'test_a.pyimport aimport pytestfrom unittest.mock import patchdef test_code(): #with patch('patch mockME somehow?', return_value='mocked code') as mock_mockME: assert_me = a.code() assert assert_me == 'mocked code'
添加回答
举报
0/150
提交
取消