vendredi 3 août 2018

Test that the exception has been raised within initializer

What is the proper way to test a scenario that while initializing my object an exception will be raised? With given snippet of code:

 def __init__(self, snmp_node: str = "0", config_file_name: str = 'config.ini'):
 [...]
 self.config_file_name = config_file_name
        try:
            self.config_parser.read(self.config_file_name)
            if len(self.config_parser.sections()) == 0:
                raise FileNotFoundError
        except FileNotFoundError:
            msg = "Error msg"
            return msg

I tried the following test:

self.assertTrue("Error msg", MyObj("0", 'nonExistingIniFile.ini')

But I got an AssertionError that init may not return str.

What is the proper way to handle such situation? Maybe some other workaround: I just want to be sure that if an user passes wrong .ini file the program won't accept that.

Aucun commentaire:

Enregistrer un commentaire