我想使用[pytest monkeypatch] [1]模拟导入的类 进入一个单独的模块。这实际上可行吗?如果可以,怎么办?似乎我没有看到这种确切情况的例子。假设您的应用已在something.py
中导入了类A。from something import A #Class is imported
class B :
def __init__(self) :
self.instance = A() #class instance is created
def f(self, value) :
return self.instance.g(value)
在我的test.py内部,我想在B内部模拟A
from something import B
#this is where I would mock A such that
def mock_A :
def g(self, value) :
return 2*value
#Then I would call B
c = B()
print(c.g(2)) #would be 4
I see how monkeypatch can be used to patch instances of classes, but how is it done for classes that have not yet been instantiated? Is it possible? Thanks!
[1]: https://docs.pytest.org/en/latest/monkeypatch.html
答案 0 :(得分:0)
测试了这个,对我有用:
shlex