How test a value in try except block with pytest and coverage in django setting?
I would like to test a Try/Except block, but I try any method Coverage that says that part is not tested. My goal is to test the Error_Message from the user's django settings, and if the user did not define file_validator_error_message in his Django settings, I would like to consider a default value (file} is not valid). Now I want to test the ERROR_MESSAGE value in the area where I test the Attributeerror error but I don't know how? The point is that in line 10 Coverage says: line 10 didn't jump to line 11, because the exception caught by line 10 didn't happen
code :
try:
# Get Error Message From Django Setting
ERROR_MESSAGE = settings.FILE_VALIDATOR_ERROR_MESSAGE
except AttributeError:
ERROR_MESSAGE = "{file} is not valid"
except ImproperlyConfigured:
ERROR_MESSAGE = "{file} is not valid"
I want to test the ERROR_MESSAGE value in the AttributeerRor error section
I corrected my code as follows and the problem was fixed :
try:
# Get Error Message From Django Setting
CUSTOM_ERROR_MESSAGE = settings.FILE_VALIDATOR_ERROR_MESSAGE
except (AttributeError, ImproperlyConfigured):
CUSTOM_ERROR_MESSAGE = "{file} is not valid"