@@ -393,6 +393,47 @@ def test_fail_if_dummy_prediction_fails(self, ta_run_mock):
393393 self ._tearDown (backend_api .temporary_directory )
394394 self ._tearDown (backend_api .output_directory )
395395
396+ @unittest .mock .patch ('autosklearn.smbo.AutoMLSMBO.run_smbo' )
397+ def test_exceptions_inside_log_in_smbo (self , smbo_run_mock ):
398+
399+ # Make sure that any exception during the AutoML fit due to
400+ # SMAC are properly captured in a log file
401+ backend_api = self ._create_backend ('test_exceptions_inside_log' )
402+ self ._tearDown (backend_api .temporary_directory )
403+ self ._tearDown (backend_api .output_directory )
404+
405+ automl = autosklearn .automl .AutoML (backend_api , 20 , 5 )
406+
407+ output_file = 'test_exceptions_inside_log.log'
408+ setup_logger (output_file = output_file )
409+ logger = get_logger ('test_exceptions_inside_log' )
410+
411+ # Create a custom exception to prevent other errors to slip in
412+ class MyException (Exception ):
413+ pass
414+
415+ X_train , Y_train , X_test , Y_test = putil .get_dataset ('iris' )
416+ # The first call is on dummy predictor failure
417+ message = str (np .random .randint (100 )) + '_run_smbo'
418+ smbo_run_mock .side_effect = MyException (message )
419+
420+ with unittest .mock .patch ('autosklearn.automl.AutoML._get_logger' ) as mock :
421+ mock .return_value = logger
422+ with self .assertRaises (MyException ):
423+ automl .fit (
424+ X_train ,
425+ Y_train ,
426+ metric = accuracy ,
427+ task = MULTICLASS_CLASSIFICATION ,
428+ )
429+ with open (output_file ) as f :
430+ self .assertTrue (message in f .read ())
431+
432+ # Cleanup
433+ os .unlink (output_file )
434+ self ._tearDown (backend_api .temporary_directory )
435+ self ._tearDown (backend_api .output_directory )
436+
396437
397438if __name__ == "__main__" :
398439 unittest .main ()
0 commit comments