【发布时间】:2020-06-13 13:08:23
【问题描述】:
假设f有条件地调用print;我想知道这是否发生在test_*() 内。这如何实现?
示例:
def f(integer): # defined in and imported from separate module
if isinstance(integer, str):
print("WARNING: integer is str")
def test_f():
f("5")
assert print.called
尝试的方法:
def tracked_call(self, *args, **kwargs):
self.called = True
self.__call__(*args, **kwargs)
print.__call__ = tracked_call
>>> AttributeError: 'builtin_function_or_method' object attribute '__call__' is read-only
【问题讨论】:
-
@KarlKnechtel 感谢您的链接;原样的答案不这样做,但我会通读它链接的文档
标签: python python-3.x pytest python-unittest