def log_header(test_function):
"""A decorator to print out which function is being tested, and its docstring"""
function_name = test_function.__name__ # type: str
function_docs_str = getdoc(test_function) # type: Optional[str]
function_docs = [] # type: List[str]
if function_docs_str:
function_docs = function_docs_str.splitlines()
def wrapper(*args, **kwargs):
rospy.loginfo("--------------------------------------------------------------------------------")
rospy.loginfo("TEST: {}".format(function_name))
if function_docs:
rospy.loginfo("TEST: {}".format(function_docs[0]))
for i in range(1,len(function_docs)):
rospy.loginfo(" {}".format(function_docs[i]))
return test_function(*args, **kwargs)
return wrapperExample usage, in a Python's unittest test function:
@log_header
def sample_test(self):
"""This is an easy test!
Don't stop riding your bike.
Will Argentina win the World Cup this year?
"""
self.assertTrue(1+1 == 2)
passOutput:
2022-01-27 18:23:01,121 INFO logger: --------------------------------------------------------------------------------
2022-01-27 18:23:01,126 INFO logger: TEST: test_sample_test
2022-01-27 18:23:01,134 INFO logger: TEST: This is an easy test!
2022-01-27 18:23:01,139 INFO logger: Don't stop riding your bike.
2022-01-27 18:23:01,142 INFO logger: Will Argentina win the World Cup this year?