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