I have a code in python as below:
import logging
def method():
value = get_value()
if value is not None:
do_something()
else:
logging.critical("value cannot be None")
I wanted to write a unit-test to cover both of the above scenarios. The first case for success was easy. But for the failure scenario, what is the correct way to catch that logging.critical was called?
I tried to put the method inside try-except block and it works.
... Mock required function and specify return values ...
try:
method()
except Exception as ex:
import traceback
traceback.format_exc()
Though the above method works, I don't think I correctly captured that logging.critical was called rather than an exception being raised. What is the correct way to go about it?