Home | History | Annotate | Download | only in test_defs
      1 #!/usr/bin/python
      2 #
      3 #
      4 # Copyright 2011, The Android Open Source Project
      5 #
      6 # Licensed under the Apache License, Version 2.0 (the "License");
      7 # you may not use this file except in compliance with the License.
      8 # You may obtain a copy of the License at
      9 #
     10 #     http://www.apache.org/licenses/LICENSE-2.0
     11 #
     12 # Unless required by applicable law or agreed to in writing, software
     13 # distributed under the License is distributed on an "AS IS" BASIS,
     14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     15 # See the License for the specific language governing permissions and
     16 # limitations under the License.
     17 
     18 """TestSuite for running C/C++ Android tests using gtest framework."""
     19 
     20 # python imports
     21 import os
     22 import re
     23 
     24 # local imports
     25 import logger
     26 import run_command
     27 import test_suite
     28 
     29 
     30 class GTestSuite(test_suite.AbstractTestSuite):
     31   """A test suite for running gtest on device."""
     32 
     33   def __init__(self):
     34     test_suite.AbstractTestSuite.__init__(self)
     35     self._target_exec_path = None
     36 
     37   def GetTargetExecPath(self):
     38     """Get the target path to gtest executable."""
     39     return self._target_exec_path
     40 
     41   def SetTargetExecPath(self, path):
     42     self._target_exec_path = path
     43     return self
     44 
     45   def Run(self, options, adb):
     46     """Run the provided gtest test suite.
     47 
     48     Args:
     49       options: command line options
     50       adb: adb interface
     51     """
     52     shell_cmd = adb.PreviewShellCommand(self.GetTargetExecPath())
     53     logger.Log(shell_cmd)
     54     if not options.preview:
     55       # gtest will log to test results to stdout, so no need to do any
     56       # extra processing
     57       run_command.RunCommand(shell_cmd, return_output=False)
     58 
     59 
     60 class GTestFactory(test_suite.AbstractTestFactory):
     61 
     62   def __init__(self, test_root_path, build_path):
     63     test_suite.AbstractTestFactory.__init__(self, test_root_path,
     64         build_path)
     65 
     66   def CreateTests(self, sub_tests_path=None):
     67     """Create tests found in sub_tests_path.
     68 
     69     Looks for test files matching a pattern, and assumes each one is a separate
     70     binary on target.
     71 
     72     Test files must match one of the following pattern:
     73       - test_*.[c|cc|cpp]
     74       - *_test.[c|cc|cpp]
     75       - *_unittest.[c|cc|cpp]
     76       - *Tests.[cc|cpp]
     77 
     78     """
     79     if not sub_tests_path:
     80       sub_tests_path = self.GetTestRootPath()
     81     test_file_list = []
     82     if os.path.isfile(sub_tests_path):
     83       self._EvaluateFile(test_file_list, os.path.basename(sub_tests_path))
     84     else:
     85       os.path.walk(sub_tests_path, self._CollectTestSources, test_file_list)
     86     # TODO: obtain this from makefile instead of hardcoding
     87     target_root_path = os.path.join('/data', 'nativetest')
     88     test_suites = []
     89     for test_file in test_file_list:
     90       logger.SilentLog('Creating gtest suite for file %s' % test_file)
     91       suite = GTestSuite()
     92       suite.SetBuildPath(self.GetBuildPath())
     93       # expect tests in /data/nativetest/test_file/test_file
     94       suite.SetTargetExecPath(os.path.join(target_root_path, test_file, test_file))
     95       test_suites.append(suite)
     96     return test_suites
     97 
     98   def _CollectTestSources(self, test_list, dirname, files):
     99     """For each directory, find tests source file and add them to the list.
    100 
    101     Test files must match one of the following pattern:
    102       - test_*.[cc|cpp]
    103       - *_test.[cc|cpp]
    104       - *_unittest.[cc|cpp]
    105       - *Tests.[cc|cpp]
    106 
    107     This method is a callback for os.path.walk.
    108 
    109     Args:
    110       test_list: Where new tests should be inserted.
    111       dirname: Current directory.
    112       files: List of files in the current directory.
    113     """
    114     for f in files:
    115       self._EvaluateFile(test_list, f)
    116 
    117   def _EvaluateFile(self, test_list, file):
    118     (name, ext) = os.path.splitext(file)
    119     if ext == ".cc" or ext == ".cpp" or ext == ".c":
    120       if re.search("_test$|_test_$|_unittest$|_unittest_$|^test_|Tests$", name):
    121         logger.SilentLog("Found native test file %s" % file)
    122         test_list.append(name)
    123