From f4a524bcc2a6d6e929ead0d593acdd0069ee3f00 Mon Sep 17 00:00:00 2001 From: Frank Brehm Date: Thu, 7 Nov 2019 17:52:25 +0100 Subject: [PATCH] Adding first unittest --- test/general.py | 138 +++++++++++++++++++++++++++++++++++++++++ test/test_01_config.py | 77 +++++++++++++++++++++++ 2 files changed, 215 insertions(+) create mode 100644 test/general.py create mode 100755 test/test_01_config.py diff --git a/test/general.py b/test/general.py new file mode 100644 index 0000000..0550f7e --- /dev/null +++ b/test/general.py @@ -0,0 +1,138 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +""" +@author: Frank Brehm +@contact: frank@brehm-online.com +@copyright: © 2019 Frank Brehm, Berlin +@license: GPL3 +@summary: general used functions an objects used for unit tests on + the base python modules +""" + +from __future__ import print_function + +import os +import sys +import logging +from logging import Formatter +import argparse + +try: + import unittest2 as unittest +except ImportError: + import unittest + +if sys.version_info[0] != 3: + print("This script is intended to use with Python3.", file=sys.stderr) + print("You are using Python: {0}.{1}.{2}-{3}-{4}.\n".format( + *sys.version_info), file=sys.stderr) + sys.exit(1) + +if sys.version_info[1] < 4: + print("A minimal Python version of 3.4 is necessary to execute this script.", file=sys.stderr) + print("You are using Python: {0}.{1}.{2}-{3}-{4}.\n".format( + *sys.version_info), file=sys.stderr) + sys.exit(1) + +# Own modules +HAS_COLORED_LOGGING = False +try: + from fb_tools.colored import ColoredFormatter + HAS_COLORED_LOGGING = True +except ImportError: + pass + +# ============================================================================= + +LOG = logging.getLogger(__name__) + + +# ============================================================================= +def get_arg_verbose(): + + arg_parser = argparse.ArgumentParser() + + arg_parser = argparse.ArgumentParser() + arg_parser.add_argument( + "-v", "--verbose", action="count", + dest='verbose', help='Increase the verbosity level') + args = arg_parser.parse_args() + + return args.verbose + + +# ============================================================================= +def init_root_logger(verbose=0): + + root_log = logging.getLogger() + root_log.setLevel(logging.INFO) + if verbose: + root_log.setLevel(logging.DEBUG) + + appname = os.path.basename(sys.argv[0]) + format_str = appname + ': ' + if verbose: + if verbose > 1: + format_str += '%(name)s(%(lineno)d) %(funcName)s() ' + else: + format_str += '%(name)s ' + format_str += '%(levelname)s - %(message)s' + formatter = None + if HAS_COLORED_LOGGING: + formatter = ColoredFormatter(format_str) + else: + formatter = Formatter(format_str) + + # create log handler for console output + lh_console = logging.StreamHandler(sys.stderr) + if verbose: + lh_console.setLevel(logging.DEBUG) + else: + lh_console.setLevel(logging.INFO) + lh_console.setFormatter(formatter) + + root_log.addHandler(lh_console) + + +# ============================================================================= +class CreateTerraformTestcase(unittest.TestCase): + + # ------------------------------------------------------------------------- + def __init__(self, methodName='runTest', verbose=0): + + self._verbose = int(verbose) + + appname = os.path.basename(sys.argv[0]).replace('.py', '') + self._appname = appname + + super(CreateTerraformTestcase, self).__init__(methodName) + + # ------------------------------------------------------------------------- + @property + def verbose(self): + """The verbosity level.""" + return getattr(self, '_verbose', 0) + + # ------------------------------------------------------------------------- + @property + def appname(self): + """The name of the current running application.""" + return self._appname + + # ------------------------------------------------------------------------- + def setUp(self): + pass + + # ------------------------------------------------------------------------- + def tearDown(self): + pass + + +# ============================================================================= +if __name__ == '__main__': + + pass + +# ============================================================================= + +# vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4 diff --git a/test/test_01_config.py b/test/test_01_config.py new file mode 100755 index 0000000..bd8d58c --- /dev/null +++ b/test/test_01_config.py @@ -0,0 +1,77 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +''' +@author: Frank Brehm +@contact: frank@brehm-online.com +@copyright: © 2019 Frank Brehm, Berlin +@license: GPL3 +@summary: test script (and module) for unit tests on config module +''' + +import os +import sys +import logging + +try: + import unittest2 as unittest +except ImportError: + import unittest + +libdir = os.path.abspath(os.path.join(os.path.dirname(__file__), '..', 'lib')) +sys.path.insert(0, libdir) + +from general import CreateTerraformTestcase, get_arg_verbose, init_root_logger + +from fb_tools.common import to_bool + +LOG = logging.getLogger('test_base_handler') + + +# ============================================================================= +class TestCreateTerraformConfig(CreateTerraformTestcase): + + # ------------------------------------------------------------------------- + def setUp(self): + + self.test_file = None + + # ------------------------------------------------------------------------- + def tearDown(self): + + if self.test_file is not None: + if os.path.exists(self.test_file): + LOG.debug("Removing {!r} ...".format(self.test_file)) + os.remove(self.test_file) + + # ------------------------------------------------------------------------- + def test_import(self): + + LOG.info("Testing import of cr_tf.config ...") + import cr_tf.config # noqa + + LOG.info("Testing import of VsphereConfig from cr_tf.config ...") + from cr_tf.config import VsphereConfig # noqa + + +# ============================================================================= +if __name__ == '__main__': + + verbose = get_arg_verbose() + if verbose is None: + verbose = 0 + init_root_logger(verbose) + + LOG.info("Starting tests ...") + + suite = unittest.TestSuite() + + suite.addTest(TestCreateTerraformConfig('test_import', verbose)) + + runner = unittest.TextTestRunner(verbosity=verbose) + + result = runner.run(suite) + + +# ============================================================================= + +# vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4 -- 2.39.5