Python 如何计算通过unittest调用内部类方法的次数?
有一种情况,我想检查一个内部类方法被调用了多少次。我有一个敏感的云任务,必须根据某些情况进行计数。我想通过一个unittest来增强我的应用程序,以断言特定函数被调用的次数 为了在更简单的场景中执行此操作,我想使用以下脚本进行测试:Python 如何计算通过unittest调用内部类方法的次数?,python,unit-testing,class,methods,assert,Python,Unit Testing,Class,Methods,Assert,有一种情况,我想检查一个内部类方法被调用了多少次。我有一个敏感的云任务,必须根据某些情况进行计数。我想通过一个unittest来增强我的应用程序,以断言特定函数被调用的次数 为了在更简单的场景中执行此操作,我想使用以下脚本进行测试: class HMT: def __init__(self): self.buildedList = [] def handle(self, string_to_explode: str): for exploded
class HMT:
def __init__(self):
self.buildedList = []
def handle(self, string_to_explode: str):
for exploded_part in string_to_explode.split(","):
self.doCoolThings(exploded_part)
def doCoolThings(self, fetched_raw_string: str):
self.buildedList.append("Cool thing done: " + fetched_raw_string)
根据我传递给handle
函数的字符串,doCoolThings
将被调用N次(在这个简单的例子中,仅取决于字符串中COMA的数量)
我可以通过计算builderList
中的结果元素数来进行测试:
import unittest
from HMT import HMT
class test_HMT(unittest.TestCase):
def setUp(self):
self.hmt = HMT()
def test_hmt_3(self):
string_to_test = "alpha,beta,gamma"
self.hmt.handle(string_to_test)
self.assertEqual(3, len(self.hmt.buildedList))
def test_hmt_2(self):
string_to_test = "delta,epsilon"
self.hmt.handle(string_to_test)
self.assertEqual(2, len(self.hmt.buildedList))
但在实际场景中,没有一个可用的公共类列表总是将其元素数量与调用函数doCoolThings
的次数相匹配
那么,如何检查调用了多少次doCoolThings
,而无需检查列表元素计数
我知道我可以在类中放置一个计数器,该计数器在每次调用doCoolThings
时都会增加,然后将其暴露在外部以供以后检查。但是,我不想把与我的业务规则没有直接关系的代码行弄乱
在@jarmod comment之后,我进入了这段代码的版本:
def mydecorator(func):
def wrapped(*args, **kwargs):
wrapped.calls += 1
return func(*args, **kwargs)
wrapped.calls = 0
return wrapped
class HMT:
def __init__(self):
self.buildedList = []
def handle(self, string_to_explode: str):
for exploded_part in string_to_explode.split(","):
self.doCoolThings(exploded_part)
@mydecorator
def doCoolThings(self, fetched_raw_string: str, *args, **kwargs):
self.buildedList.append("Cool thing done: " + fetched_raw_string)
以及测试:
import unittest
from HMT import HMT
class test_HMT(unittest.TestCase):
def test_hmt_3_dec(self):
hmt = HMT()
string_to_test = "epsilon,ota,eta"
hmt.handle(string_to_test)
self.assertEqual(3, hmt.doCoolThings.calls)
def test_hmt_3(self):
hmt = HMT()
string_to_test = "alpha,beta,gamma"
hmt.handle(string_to_test)
self.assertEqual(3, len(hmt.buildedList))
但仍然不能正常工作。当我运行测试时,我收到:
.F
======================================================================
FAIL: test_hmt_3_dec (myTest.test_HMT)
----------------------------------------------------------------------
Traceback (most recent call last):
File "D:\Users\danil\tmp\myDec\myTest.py", line 10, in test_hmt_3_dec
self.assertEqual(3, hmt.doCoolThings.calls)
AssertionError: 3 != 6
----------------------------------------------------------------------
Ran 2 tests in 0.001s
FAILED (failures=1)
更多的测试显示测试运行了两次,但在新的实例化中计数器也不会重置
无论如何,最初的想法是在内部类方法中动态放置一个观察者,并在每次触发这些方法时进行外部获取(尽管如此,这似乎不是使用decorator解决的问题)
非常感谢您的回答(另外,如果有人知道如何在装饰器中重置计数器,我也将非常感谢您保持通电状态)
- doCoolThings未在业务代码中修改。您只需获得用于测试的计数行为
- 您可以通过用对象替换count变量来摆脱全局变量。。或者通过做任何其他事情。但这在测试中重要吗
- doCoolThings未在业务代码中修改。您只需获得用于测试的计数行为
- 您可以通过用对象替换count变量来摆脱全局变量。。或者通过做任何其他事情。但这在测试中重要吗
from HMT import HMT
count = 0
def counter(f):
def wrap(*args, **kwargs):
global count
count += 1
return f(*args, **kwargs)
return wrap
class test_HMT(unittest.TestCase):
def setUp(self):
self.hmt = HMT()
# Add decorator.
self.hmt_no_decorator = self.hmt.doCoolThings
self.hmt.doCoolThings = counter(self.hmt.doCoolThings)
def test_doCoolThings_count(self):
repeat = 3
[self.hmt.doCoolThings() for _ in range(repeat)]
self.assertEqual(counter, repeat)
def tearDown(self):
# Remove decorator.
self.hmt.doCoolThings = self.hmt_no_decorator
...