不确定这是否可行,但有没有办法模拟 eval 导入的函数?
例如:
一个.py
import b
def code():
me = 'ME'
should_be_changed = eval('b.mock' + me + '()')
return should_be_changed
b.py
def mockME():
return 'Dummy code'
test_a.py
import a
import pytest
from unittest.mock import patch
def test_code():
#with patch('patch mockME somehow?', return_value='mocked code') as mock_mockME:
assert_me = a.code()
assert assert_me == 'mocked code'
相关分类