Esempio n. 1
0
 def test_usage_and_exit_error_handler(self):
     with _suppress_stderr():
         parser = ArgumentParser()
         parser.add_argument('--val', type=int)
         self.assertEqual(8, parser.parse_args(['--val', '8']).val)
         self.assertRaises(SystemExit,
                           lambda: parser.parse_args(['--val', 'eight']))
Esempio n. 2
0
 def test_logger_levels(self):
     levels = ['DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL']
     for num, level in enumerate(levels):
         with self.subTest(level), _suppress_stderr():
             test = self.TestClass(logger={'level': level})
             with self.assertLogs(level=level) as log:
                 getattr(test.logger, level.lower())(self.log_message)
                 self.assertEqual(len(log.output), 1)
                 self.assertIn(self.log_message, log.output[0])
             if num > 0:
                 with self.assertRaises(AssertionError), self.assertLogs():
                     getattr(test.logger, levels[num-1].lower())(self.log_message)
Esempio n. 3
0
    def test_logger_debug(self):

        with _suppress_stderr():

            class Class1:
                def __init__(self, c1_a1: float, c1_a2: int = 1):
                    pass

            class Class2(Class1):
                def __init__(self,
                             *args,
                             c2_a1: int = 2,
                             c2_a2: float = 0.2,
                             **kwargs):
                    pass

            parser = ArgumentParser(error_handler=None,
                                    logger={'level': 'DEBUG'})
            with self.assertLogs(level='DEBUG') as log:
                parser.add_class_arguments(Class2, skip={'c2_a2'})
                self.assertEqual(1, len(log.output))
                self.assertIn('"c2_a2" from "Class2"', log.output[0])
                self.assertIn('Parameter requested to be skipped',
                              log.output[0])

            class Class3(Class1):
                def __init__(self, *args):
                    pass

            parser = ArgumentParser(error_handler=None,
                                    logger={'level': 'DEBUG'})
            with self.assertLogs(level='DEBUG') as log:
                parser.add_class_arguments(Class3)
                self.assertEqual(1, len(log.output))
                self.assertIn('"c1_a2" from "Class1"', log.output[0])
                self.assertIn('Keyword parameter but **kwargs not propagated',
                              log.output[0])

            class Class4(Class1):
                def __init__(self, **kwargs):
                    pass

            parser = ArgumentParser(error_handler=None,
                                    logger={'level': 'DEBUG'})
            with self.assertLogs(level='DEBUG') as log:
                parser.add_class_arguments(Class4)
                self.assertEqual(1, len(log.output))
                self.assertIn('"c1_a1" from "Class1"', log.output[0])
                self.assertIn('Positional parameter but *args not propagated',
                              log.output[0])