我似乎无法重现你的问题(我已经稍微调整了你的例子,因为它根本没有运行).你有没有错误的东西(比如mymodule._b而不是mymodule .__ b)?
mymodule.py:
def a(x):
return __b("someval")
def __b(args):
return "complex_thingy: {}".format(args)
mytest.py:
from unittest import TestCase
import mymodule
def newfn(args):
return {"a" : "b"}
mymodule.__b = newfn
class TestMyModule(TestCase):
def test_basic(self):
print(mymodule.a('somearg'))
输出:
C:\TEMP>python -m unittest mytest
{'a': 'b'}
.
----------------------------------------------------------------------
Ran 1 test in 0.001s
OK
C:\TEMP>
似乎工作正常.
或者在unittest之外:
mytest2.py:
import mymodule
def newfn(args):
return {"a" : "b"}
mymodule.__b = newfn
print(mymodule.a('somearg'))
输出:
C:\TEMP>python mytest2.py
{'a': 'b'}
C:\TEMP>