takashi kadono / Mbed OS Nucleo_446

Dependencies:   ssd1331

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers mbed_unittest.py Source File

mbed_unittest.py

00001 #!/usr/bin/env python
00002 
00003 """
00004 Copyright (c) 2018, Arm Limited
00005 SPDX-License-Identifier: Apache-2.0
00006 
00007 Licensed under the Apache License, Version 2.0 (the "License");
00008 you may not use this file except in compliance with the License.
00009 You may obtain a copy of the License at
00010 
00011     http://www.apache.org/licenses/LICENSE-2.0
00012 
00013 Unless required by applicable law or agreed to in writing, software
00014 distributed under the License is distributed on an "AS IS" BASIS,
00015 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00016 See the License for the specific language governing permissions and
00017 limitations under the License.
00018 
00019 
00020 UNIT TEST BUILD & RUN
00021 """
00022 
00023 from __future__ import print_function
00024 import os
00025 import logging
00026 import re
00027 
00028 from unit_test.options import get_options_parser, \
00029                               pretty_print_test_options
00030 from unit_test.settings import DEFAULT_CMAKE_GENERATORS
00031 from unit_test.test import UnitTestTool
00032 from unit_test.new import UnitTestGeneratorTool
00033 from unit_test.coverage import CoverageAPI
00034 
00035 def _mbed_unittest_test(options, cwd, pwd):
00036     if options is None:
00037         return
00038 
00039     if options.coverage:
00040         options.debug_build = True
00041 
00042     if options.cmake_generator is None:
00043         options.cmake_generator = DEFAULT_CMAKE_GENERATORS.get(
00044             options.make_program,
00045             "Unix Makefiles")
00046 
00047     # Do not clean directory if only run.
00048     if options.clean and options.run_only and not options.compile_only:
00049         options.clean = False
00050 
00051     # Build and run by default
00052     if (not options.compile_only and
00053             not options.run_only and
00054             not options.clean):
00055         options.compile_only = True
00056         options.run_only = True
00057 
00058     # Set build path
00059     if not os.path.isabs(options.build):
00060         options.build = os.path.normpath(
00061             os.path.join(cwd, options.build))
00062 
00063     pretty_print_test_options(options)
00064 
00065     # Create test tool
00066     tool = UnitTestTool(make_program=options.make_program)
00067 
00068     # Prepare build directory
00069     tool.prepare_build_directory(path_to_src=pwd,
00070                                  build_path=options.build,
00071                                  clean=options.clean)
00072 
00073     if options.compile_only:
00074         # Create makefiles
00075         src_path = os.path.relpath(pwd, options.build)
00076         tool.create_makefiles(path_to_src=src_path,
00077                               generator=options.cmake_generator,
00078                               coverage_output_type=options.coverage,
00079                               debug=options.debug_build)
00080 
00081         # Build tests
00082         tool.build_tests()
00083 
00084     if options.run_only:
00085         tool.run_tests(filter_regex=options.test_regex)
00086 
00087         # If code coverage generation:
00088         if options.coverage:
00089             cov_api = CoverageAPI(
00090                 mbed_os_root=os.path.normpath(os.path.join(pwd, "..")),
00091                 build_dir=options.build)
00092 
00093             # Generate reports
00094             outputs = [options.coverage]
00095             if options.coverage == "both":
00096                 outputs = ["html", "xml"]
00097 
00098             excludes = [pwd, options.build]
00099 
00100             if not options.include_headers:
00101                 excludes.append(re.compile(".*\\.h"))
00102 
00103             cov_api.generate_reports(outputs=outputs,
00104                                      excludes=excludes,
00105                                      filter_regex=options.test_regex,
00106                                      build_path=options.build)
00107 
00108 def _mbed_unittest_new(options, pwd):
00109     if options is None:
00110         return
00111 
00112     generator = UnitTestGeneratorTool()
00113 
00114     mbed_os_root = os.path.normpath(
00115         os.path.join(pwd, "..")
00116     )
00117 
00118     for filepath in options.new_files:
00119         generator.create(
00120             mbed_os_root=mbed_os_root,
00121             filepath=filepath)
00122 
00123 def mbed_unittest(options=None):
00124     """
00125     Create, build and run unit tests
00126     """
00127 
00128     if options is None:
00129         return
00130 
00131     cwd = os.getcwd()
00132 
00133     # Change current working directory to script location
00134     unittest_dir = os.path.dirname(os.path.realpath(__file__))
00135     os.chdir(unittest_dir)
00136 
00137     if options.new_files:
00138         _mbed_unittest_new(options, unittest_dir)
00139     else:
00140         _mbed_unittest_test(options, cwd, unittest_dir)
00141 
00142 def mbed_unittest_main():
00143     """
00144     Create, build and run unit tests - main
00145     """
00146 
00147     # Get parser and parse command line arguments
00148     parser = get_options_parser()
00149     options = parser.parse_args()
00150 
00151     # Setup logger
00152     if options.log_level:
00153         logging.basicConfig(level=getattr(logging, options.log_level),
00154                             format="%(message)s")
00155     else:
00156         logging.basicConfig(level=logging.INFO,
00157                             format="%(message)s")
00158 
00159     mbed_unittest(options)
00160 
00161 if __name__ == "__main__":
00162     mbed_unittest_main()