Beispiel #1
0
class TestErrorFunctions(unittest.TestCase):
    def setUp(self):
        # Instantiate an Error object
        self.error = Error("This is an error.")

    def tearDown(self):
        # Release instantiated objects
        del self.error

    def test_init(self):
        self.assertEqual(self.error.msg, "This is an error.")
        self.assertIsNone(self.error.excp)
        # The caller is the Error instantiation of the setUp() function, so please update these lines if needed
        self.assertEqual(self.error.frame_info.filename,
                         utest_path + 'test_utils_error_handling.py')
        self.assertEqual(self.error.frame_info.lineno, 16)
        self.assertEqual(self.error.frame_info.function, 'setUp')
        self.assertListEqual(
            self.error.frame_info.code_context,
            ['        self.error = Error("This is an error.")\n'])

    def test_str(self):
        self.assertEqual(
            str(self.error), "  File \"" + utest_path +
            "test_utils_error_handling.py\", line 16, in setUp" + EOL +
            "    Error: This is an error.")

    def test_handle(self):
        test = False
        try:
            print
            self.error.handle()
        except SystemExit:
            test = True
        self.assertTrue(test)
class TestErrorFunctions(unittest.TestCase):

    def setUp(self):
        # Instantiate an Error object
        self.error = Error("This is an error.")

    def tearDown(self):
        # Release instantiated objects
        del self.error

    def test_init(self):
        self.assertEqual(self.error.msg, "This is an error.")
        self.assertIsNone(self.error.excp)
        # The caller is the Error instantiation of the setUp() function, so please update these lines if needed
        self.assertEqual(self.error.frame_info.filename, utest_path + 'test_utils_error_handling.py')
        self.assertEqual(self.error.frame_info.lineno, 16)
        self.assertEqual(self.error.frame_info.function, 'setUp')
        self.assertListEqual(self.error.frame_info.code_context, ['        self.error = Error("This is an error.")\n'])

    def test_str(self):
        self.assertEqual(str(self.error), "  File \"" + utest_path + "test_utils_error_handling.py\", line 16, in setUp" + EOL + "    Error: This is an error.")

    def test_handle(self):
        test = False
        try:
            print
            self.error.handle()
        except SystemExit:
            test = True
        self.assertTrue(test)
Beispiel #3
0
def log(msg, options=None):
    """! @brief Write message into log file if any, or to standard output if verbose mode is on.
    @param msg String to log.
    @param options User options.
    """
    try:
        import sys
        ## If provided, set function variables according to user options
        if options is not None:
            # Keep log filename and verbose mode in function variables
            setattr(log, "log_filename", options.log_filename)
            setattr(log, "verbose", options.verbose)
            # Initialize log file
            if log.log_filename is not None:
                log_file = open_file(log.log_filename, 'w+')
                log_file.close()
        ## Prepare message to log: add end of line to message
        msg += EOL
        ## Depending on options, log into file or standard output
        if hasattr(log, "log_filename") and log.log_filename is not None:
            # Open log file
            log_file = open_file(log.log_filename, 'a')
            # Write message into log file
            log_file.write(msg)
            # Close log file
            log_file.close()
        # If no log filename has been specified, check if verbose mode has been set by user
        elif hasattr(log, "verbose") and log.verbose:
            sys.stdout.write(msg)
    except IOError as exception:
        raise Error("Cannot write into log file '%s'." % options.log_filename,
                    exception)
Beispiel #4
0
def open_file(filename, mode, encoding=ENCODING):
    """! @brief Open file in specified mode (automatically decode file in unicode).
    @param filename Full path to file to open.
    @param mode Read or write mode.
    @param encoding Encoding mode. Default value is 'utf-8'.
    @return File handler.
    """
    try:
        try:
            return open(filename, mode, encoding=encoding)
        except TypeError:
            import codecs
            return codecs.open(filename, mode, encoding=encoding)
    except IOError as exception:
        raise Error("Cannot open file.", exception)
Beispiel #5
0
 def setUp(self):
     # Instantiate an Error object
     self.error = Error("This is an error.")
 def setUp(self):
     # Instantiate an Error object
     self.error = Error("This is an error.")