PageRenderTime 44ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/development/testrunner/test_defs/instrumentation_test.py

https://gitlab.com/brian0218/rk3188_rk3066_r-box_android4.4.2_sdk
Python | 364 lines | 225 code | 38 blank | 101 comment | 47 complexity | 80c08dd590a0c2e9091d5f046e9c5aca MD5 | raw file
  1. #!/usr/bin/python2.4
  2. #
  3. #
  4. # Copyright 2008, 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. """TestSuite definition for Android instrumentation tests."""
  18. import os
  19. import re
  20. # local imports
  21. import android_manifest
  22. from coverage import coverage
  23. import errors
  24. import logger
  25. import test_suite
  26. class InstrumentationTestSuite(test_suite.AbstractTestSuite):
  27. """Represents a java instrumentation test suite definition run on device."""
  28. DEFAULT_RUNNER = "android.test.InstrumentationTestRunner"
  29. def __init__(self):
  30. test_suite.AbstractTestSuite.__init__(self)
  31. self._package_name = None
  32. self._runner_name = self.DEFAULT_RUNNER
  33. self._class_name = None
  34. self._target_name = None
  35. self._java_package = None
  36. def GetPackageName(self):
  37. return self._package_name
  38. def SetPackageName(self, package_name):
  39. self._package_name = package_name
  40. return self
  41. def GetRunnerName(self):
  42. return self._runner_name
  43. def SetRunnerName(self, runner_name):
  44. self._runner_name = runner_name
  45. return self
  46. def GetClassName(self):
  47. return self._class_name
  48. def SetClassName(self, class_name):
  49. self._class_name = class_name
  50. return self
  51. def GetJavaPackageFilter(self):
  52. return self._java_package
  53. def SetJavaPackageFilter(self, java_package_name):
  54. """Configure the suite to only run tests in given java package."""
  55. self._java_package = java_package_name
  56. return self
  57. def GetTargetName(self):
  58. """Retrieve module that this test is targeting.
  59. Used for generating code coverage metrics.
  60. Returns:
  61. the module target name
  62. """
  63. return self._target_name
  64. def SetTargetName(self, target_name):
  65. self._target_name = target_name
  66. return self
  67. def GetBuildDependencies(self, options):
  68. if options.coverage_target_path:
  69. return [options.coverage_target_path]
  70. return []
  71. def Run(self, options, adb):
  72. """Run the provided test suite.
  73. Builds up an adb instrument command using provided input arguments.
  74. Args:
  75. options: command line options to provide to test run
  76. adb: adb_interface to device under test
  77. Raises:
  78. errors.AbortError: if fatal error occurs
  79. """
  80. test_class = self.GetClassName()
  81. if options.test_class is not None:
  82. test_class = options.test_class.lstrip()
  83. if test_class.startswith("."):
  84. test_class = self.GetPackageName() + test_class
  85. if options.test_method is not None:
  86. test_class = "%s#%s" % (test_class, options.test_method)
  87. test_package = self.GetJavaPackageFilter()
  88. if options.test_package:
  89. test_package = options.test_package
  90. if test_class and test_package:
  91. logger.Log('Error: both class and java package options are specified')
  92. instrumentation_args = {}
  93. if test_class is not None:
  94. instrumentation_args["class"] = test_class
  95. if test_package:
  96. instrumentation_args["package"] = test_package
  97. if options.test_size:
  98. instrumentation_args["size"] = options.test_size
  99. if options.wait_for_debugger:
  100. instrumentation_args["debug"] = "true"
  101. if options.suite_assign_mode:
  102. instrumentation_args["suiteAssignment"] = "true"
  103. if options.coverage:
  104. instrumentation_args["coverage"] = "true"
  105. if options.test_annotation:
  106. instrumentation_args["annotation"] = options.test_annotation
  107. if options.test_not_annotation:
  108. instrumentation_args["notAnnotation"] = options.test_not_annotation
  109. if options.preview:
  110. adb_cmd = adb.PreviewInstrumentationCommand(
  111. package_name=self.GetPackageName(),
  112. runner_name=self.GetRunnerName(),
  113. raw_mode=options.raw_mode,
  114. instrumentation_args=instrumentation_args)
  115. logger.Log(adb_cmd)
  116. elif options.coverage:
  117. coverage_gen = coverage.CoverageGenerator(adb)
  118. if options.coverage_target_path:
  119. coverage_target = coverage_gen.GetCoverageTargetForPath(options.coverage_target_path)
  120. elif self.GetTargetName():
  121. coverage_target = coverage_gen.GetCoverageTarget(self.GetTargetName())
  122. self._CheckInstrumentationInstalled(adb)
  123. # need to parse test output to determine path to coverage file
  124. logger.Log("Running in coverage mode, suppressing test output")
  125. try:
  126. (test_results, status_map) = adb.StartInstrumentationForPackage(
  127. package_name=self.GetPackageName(),
  128. runner_name=self.GetRunnerName(),
  129. timeout_time=60*60,
  130. instrumentation_args=instrumentation_args)
  131. except errors.InstrumentationError, errors.DeviceUnresponsiveError:
  132. return
  133. self._PrintTestResults(test_results)
  134. device_coverage_path = status_map.get("coverageFilePath", None)
  135. if device_coverage_path is None:
  136. logger.Log("Error: could not find coverage data on device")
  137. return
  138. coverage_file = coverage_gen.ExtractReport(
  139. self.GetName(), coverage_target, device_coverage_path,
  140. test_qualifier=options.test_size)
  141. if coverage_file is not None:
  142. logger.Log("Coverage report generated at %s" % coverage_file)
  143. else:
  144. self._CheckInstrumentationInstalled(adb)
  145. adb.StartInstrumentationNoResults(package_name=self.GetPackageName(),
  146. runner_name=self.GetRunnerName(),
  147. raw_mode=options.raw_mode,
  148. instrumentation_args=
  149. instrumentation_args)
  150. def _CheckInstrumentationInstalled(self, adb):
  151. if not adb.IsInstrumentationInstalled(self.GetPackageName(),
  152. self.GetRunnerName()):
  153. msg=("Could not find instrumentation %s/%s on device. Try forcing a "
  154. "rebuild by updating a source file, and re-executing runtest." %
  155. (self.GetPackageName(), self.GetRunnerName()))
  156. raise errors.AbortError(msg=msg)
  157. def _PrintTestResults(self, test_results):
  158. """Prints a summary of test result data to stdout.
  159. Args:
  160. test_results: a list of am_instrument_parser.TestResult
  161. """
  162. total_count = 0
  163. error_count = 0
  164. fail_count = 0
  165. for test_result in test_results:
  166. if test_result.GetStatusCode() == -1: # error
  167. logger.Log("Error in %s: %s" % (test_result.GetTestName(),
  168. test_result.GetFailureReason()))
  169. error_count+=1
  170. elif test_result.GetStatusCode() == -2: # failure
  171. logger.Log("Failure in %s: %s" % (test_result.GetTestName(),
  172. test_result.GetFailureReason()))
  173. fail_count+=1
  174. total_count+=1
  175. logger.Log("Tests run: %d, Failures: %d, Errors: %d" %
  176. (total_count, fail_count, error_count))
  177. def HasInstrumentationTest(path):
  178. """Determine if given path defines an instrumentation test.
  179. Args:
  180. path: file system path to instrumentation test.
  181. """
  182. manifest_parser = android_manifest.CreateAndroidManifest(path)
  183. if manifest_parser:
  184. return manifest_parser.GetInstrumentationNames()
  185. return False
  186. class InstrumentationTestFactory(test_suite.AbstractTestFactory):
  187. """A factory for creating InstrumentationTestSuites"""
  188. def __init__(self, test_root_path, build_path):
  189. test_suite.AbstractTestFactory.__init__(self, test_root_path,
  190. build_path)
  191. def CreateTests(self, sub_tests_path=None):
  192. """Create tests found in test_path.
  193. Will create a single InstrumentationTestSuite based on info found in
  194. AndroidManifest.xml found at build_path. Will set additional filters if
  195. test_path refers to a java package or java class.
  196. """
  197. tests = []
  198. class_name_arg = None
  199. java_package_name = None
  200. if sub_tests_path:
  201. # if path is java file, populate class name
  202. if self._IsJavaFile(sub_tests_path):
  203. class_name_arg = self._GetClassNameFromFile(sub_tests_path)
  204. logger.SilentLog('Using java test class %s' % class_name_arg)
  205. elif self._IsJavaPackage(sub_tests_path):
  206. java_package_name = self._GetPackageNameFromDir(sub_tests_path)
  207. logger.SilentLog('Using java package %s' % java_package_name)
  208. try:
  209. manifest_parser = android_manifest.AndroidManifest(app_path=
  210. self.GetTestsRootPath())
  211. instrs = manifest_parser.GetInstrumentationNames()
  212. if not instrs:
  213. logger.Log('Could not find instrumentation declarations in %s at %s' %
  214. (android_manifest.AndroidManifest.FILENAME,
  215. self.GetBuildPath()))
  216. return tests
  217. elif len(instrs) > 1:
  218. logger.Log("Found multiple instrumentation declarations in %s/%s. "
  219. "Only using first declared." %
  220. (self.GetBuildPath(),
  221. android_manifest.AndroidManifest.FILENAME))
  222. instr_name = manifest_parser.GetInstrumentationNames()[0]
  223. # escape inner class names
  224. instr_name = instr_name.replace('$', '\$')
  225. pkg_name = manifest_parser.GetPackageName()
  226. if instr_name.find(".") < 0:
  227. instr_name = "." + instr_name
  228. logger.SilentLog('Found instrumentation %s/%s' % (pkg_name, instr_name))
  229. suite = InstrumentationTestSuite()
  230. suite.SetPackageName(pkg_name)
  231. suite.SetBuildPath(self.GetBuildPath())
  232. suite.SetRunnerName(instr_name)
  233. suite.SetName(pkg_name)
  234. suite.SetClassName(class_name_arg)
  235. suite.SetJavaPackageFilter(java_package_name)
  236. # this is a bit of a hack, assume if 'com.android.cts' is in
  237. # package name, this is a cts test
  238. # this logic can be removed altogether when cts tests no longer require
  239. # custom build steps
  240. if suite.GetPackageName().startswith('com.android.cts'):
  241. suite.SetSuite('cts')
  242. tests.append(suite)
  243. return tests
  244. except:
  245. logger.Log('Could not find or parse %s at %s' %
  246. (android_manifest.AndroidManifest.FILENAME,
  247. self.GetBuildPath()))
  248. return tests
  249. def _IsJavaFile(self, path):
  250. """Returns true if given file system path is a java file."""
  251. return os.path.isfile(path) and self._IsJavaFileName(path)
  252. def _IsJavaFileName(self, filename):
  253. """Returns true if given file name is a java file name."""
  254. return os.path.splitext(filename)[1] == '.java'
  255. def _IsJavaPackage(self, path):
  256. """Returns true if given file path is a java package.
  257. Currently assumes if any java file exists in this directory, than it
  258. represents a java package.
  259. Args:
  260. path: file system path of directory to check
  261. Returns:
  262. True if path is a java package
  263. """
  264. if not os.path.isdir(path):
  265. return False
  266. for file_name in os.listdir(path):
  267. if self._IsJavaFileName(file_name):
  268. return True
  269. return False
  270. def _GetClassNameFromFile(self, java_file_path):
  271. """Gets the fully qualified java class name from path.
  272. Args:
  273. java_file_path: file system path of java file
  274. Returns:
  275. fully qualified java class name or None.
  276. """
  277. package_name = self._GetPackageNameFromFile(java_file_path)
  278. if package_name:
  279. filename = os.path.basename(java_file_path)
  280. class_name = os.path.splitext(filename)[0]
  281. return '%s.%s' % (package_name, class_name)
  282. return None
  283. def _GetPackageNameFromDir(self, path):
  284. """Gets the java package name associated with given directory path.
  285. Caveat: currently just parses defined java package name from first java
  286. file found in directory.
  287. Args:
  288. path: file system path of directory
  289. Returns:
  290. the java package name or None
  291. """
  292. for filename in os.listdir(path):
  293. if self._IsJavaFileName(filename):
  294. return self._GetPackageNameFromFile(os.path.join(path, filename))
  295. def _GetPackageNameFromFile(self, java_file_path):
  296. """Gets the java package name associated with given java file path.
  297. Args:
  298. java_file_path: file system path of java file
  299. Returns:
  300. the java package name or None
  301. """
  302. logger.SilentLog('Looking for java package name in %s' % java_file_path)
  303. re_package = re.compile(r'package\s+(.*);')
  304. file_handle = open(java_file_path, 'r')
  305. for line in file_handle:
  306. match = re_package.match(line)
  307. if match:
  308. return match.group(1)
  309. return None