0

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.

hikamare
  • 304
  • 1
  • 14

1 Answers1

0

init is required to return None. I think you are looking for self.assertRaises

with self.assertRaises(FileNotFoundError):
     MyObj("0", 'nonExistingIniFile.ini')
joextodd
  • 694
  • 4
  • 9
  • I already tried that but assertRaises passes the test only if the exception is not handled. When I do try: except: block and handle that exception the test fails as the exception was never raised. – hikamare Aug 03 '18 at 10:59
  • You should remove the try: except block, it has no purpose here other than returning an error string, which you cannot do in a constructor. If your code has experienced an error, it should raise it. – joextodd Aug 03 '18 at 11:09
  • Indeed it should but shouldn't I somehow handle that exception so it doesn't crash my whole app? – hikamare Aug 03 '18 at 11:21
  • If no config file is passed in, then I would argue that the app should crash. Unless you can set up some defaults in the except block. – joextodd Aug 03 '18 at 11:43
  • It would crash if I don't handle the exception which I am rising, I am right? – hikamare Aug 03 '18 at 12:01
  • Yes that's right. It all depends if your program will work without the config file or not. If it will not function without a config file, then it should raise an exception when it doesn't get one. There is no point in raising an exception only to catch it and do nothing. – joextodd Aug 03 '18 at 12:19
  • That is clear. It will not work without that config.ini file and I just wanted to have that covered in my tests that if user doesn't provide proper config file the program should behave somehow else that with that file. And the whole point of returning that msg was to notify user that something went wrong and he has to provide different config file (or any of one wasn't provided). – hikamare Aug 03 '18 at 12:48
  • Using the Zen of Python paradigm - explicit is better than implicit, I would also change your code to be more explicit about the error raised. When config_parser.sections() == 0, in fact the file has been found, it is the content of the file that cannot be decoded. Perhaps raising a ValueError would be more suited. Also you can specify the error string to be raised like so. raise ValueError('The config file could not be parsed') – joextodd Aug 03 '18 at 13:02
  • That error msg is just an example that IF the exception is handled the test fails. About the kind of exception: the .sections() == 0 condition is used because while I tried to use config_parser.read() on a file that does not exist the exception wasn't raised. In this case I believe that it is really not that important what kind of exception I raise. – hikamare Aug 03 '18 at 13:59