Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1669)

Unified Diff: build/android/pylib/instrumentation/instrumentation_test_instance.py

Issue 2935503002: List Java Instru Test Information From JUnit Runner (Closed)
Patch Set: add tests Created 3 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: build/android/pylib/instrumentation/instrumentation_test_instance.py
diff --git a/build/android/pylib/instrumentation/instrumentation_test_instance.py b/build/android/pylib/instrumentation/instrumentation_test_instance.py
index 2443384e51864c28c3a3c22d2b19f34704ae8aca..06f37e78521460de2d8c7d6f700e796b87758b70 100644
--- a/build/android/pylib/instrumentation/instrumentation_test_instance.py
+++ b/build/android/pylib/instrumentation/instrumentation_test_instance.py
@@ -3,12 +3,14 @@
# found in the LICENSE file.
import copy
+import json
import logging
import os
import pickle
import re
from devil.android import apk_helper
+from devil.android import device_temp_file
from devil.android import md5sum
from pylib import constants
from pylib.base import base_test_result
@@ -20,6 +22,7 @@ from pylib.instrumentation import instrumentation_parser
from pylib.utils import dexdump
from pylib.utils import proguard
from pylib.utils import shared_preference_utils
+from py_utils import tempfile_ext
with host_paths.SysPath(host_paths.BUILD_COMMON_PATH):
import unittest_util # pylint: disable=import-error
@@ -45,6 +48,8 @@ _EXTRA_DRIVER_TARGET_CLASS = (
'org.chromium.test.driver.OnDeviceInstrumentationDriver.TargetClass')
_EXTRA_TIMEOUT_SCALE = (
'org.chromium.test.driver.OnDeviceInstrumentationDriver.TimeoutScale')
+_EXTRA_TEST_LIST = (
+ 'org.chromium.base.test.BaseChromiumAndroidJUnitRunner.TestList')
_SKIP_PARAMETERIZATION = 'SkipCommandLineParameterization'
_COMMANDLINE_PARAMETERIZATION = 'CommandLineParameter'
@@ -162,8 +167,8 @@ def FilterTests(tests, test_filter=None, annotations=None,
Args:
tests: a list of tests. e.g. [
- {'annotations": {}, 'class': 'com.example.TestA', 'methods':[]},
- {'annotations": {}, 'class': 'com.example.TestB', 'methods':[]}]
+ {'annotations": {}, 'class': 'com.example.TestA', 'method':'test1'},
+ {'annotations": {}, 'class': 'com.example.TestB', 'method':'test2'}]
test_filter: googletest-style filter string.
annotations: a dict of wanted annotations for test methods.
exclude_annotations: a dict of annotations to exclude.
@@ -185,6 +190,12 @@ def FilterTests(tests, test_filter=None, annotations=None,
GetUniqueTestName(t, sep='.')
]
+ if t['is_junit4']:
+ names += [
+ GetTestNameWithoutParameterPostfix(t, sep='.'),
+ GetTestNameWithoutParameterPostfix(unqualified_class_test, sep='.')
+ ]
+
pattern_groups = test_filter.split('-')
if len(pattern_groups) > 1:
negative_filter = pattern_groups[1]
@@ -239,6 +250,21 @@ def FilterTests(tests, test_filter=None, annotations=None,
return filtered_tests
+def GetAllTestsFromRunner(device, test_apk, test_package,
+ test_runner=None, test_runner_junit4=None):
+ pickle_path = '%s-runner.pickle' % test_apk.path
+ try:
+ tests = _GetTestsFromPickle(pickle_path, test_apk.path)
+ except TestListPickleException as e:
+ logging.info('Could not get tests from pickle: %s', e)
+ logging.info('Getting tests by print tests in instrumentation runner')
+ tests = _GetAllTestsFromRunner(device, test_package, test_runner,
+ test_runner_junit4)
+ _SaveTestsToPickle(pickle_path, test_apk.path, tests)
+ return tests
+
+
+# TODO(yolandyan): remove this once the tests are converted to junit4
def GetAllTestsFromJar(test_jar):
pickle_path = '%s-proguard.pickle' % test_jar
try:
@@ -336,6 +362,32 @@ def _GetTestsFromDexdump(test_apk):
})
return tests
+def _GetAllTestsFromRunner(device, test_package, test_runner=None,
+ test_runner_junit4=None):
+ with device_temp_file.DeviceTempFile(
+ device.adb, suffix='.json', dir=device.GetExternalStoragePath()) as f:
+ if test_runner is None and test_runner_junit4 is None:
mikecase (-- gone --) 2017/07/18 23:59:12 test_runner basically unused here.
the real yoland 2017/07/19 00:12:49 Removed
+ raise Exception('Test runner does NOT exist for this test apk')
+ if test_runner_junit4:
+ extras = {}
+ extras[_EXTRA_TEST_LIST] = f.name
+ extras['log'] = 'true'
+ extras['package'] = 'org.chromium'
mikecase (-- gone --) 2017/07/18 23:59:12 so this won't work downstream right? I think some
the real yoland 2017/07/19 00:12:49 Changed This is just for the sake of speed up java
+ target = '%s/%s' % (test_package, test_runner_junit4)
+ output_string = ''.join(device.StartInstrumentation(
+ target, extras=extras, timeout=30, retries=2))
+ if output_string:
+ raise Exception('Test listing through %s failed on device:\n%s' % (
+ test_runner_junit4, output_string))
+ else:
+ raise Exception('JUnit4 Runner is not present in this apk')
+ with tempfile_ext.NamedTemporaryDirectory() as host_dir:
+ host_file = os.path.join(host_dir, 'list_tests.json')
+ device.PullFile(f.name, host_file)
+ with open(host_file, 'r') as host_file:
+ json_string = host_file.read()
+ return json.loads(json_string)
+
def _SaveTestsToPickle(pickle_path, jar_path, tests):
jar_md5 = md5sum.CalculateHostMd5Sums(jar_path)[jar_path]
@@ -379,6 +431,28 @@ def GetTestName(test, sep='#'):
return '%s%s%s' % (test['class'], sep, test['method'])
+def GetTestNameWithoutParameterPostfix(
+ test, sep='#', parameter_postfix='__'):
mikecase (-- gone --) 2017/07/18 23:59:12 prefix? Or is it more of a parameter_postfix_prefi
the real yoland 2017/07/19 00:12:49 woah troll
+ """Gets the name of the given JUnit4 test withouth parameter postfix.
mikecase (-- gone --) 2017/07/18 23:59:12 withouth
the real yoland 2017/07/19 00:12:49 Done
+
+ For most WebView JUnit4 javatests, each test is parameterizatized with
+ "__sandboxed_mode" to run in both non-sandboxed mode and sandboxed mode.
+
+ This function returns the name of the test without parameterization
+ so test filters can match both parameterized and non-parameterized tests.
+
+ Args:
+ test: the instrumentation test dict.
+ sep: the character(s) that should join the class name and the method name.
+ parameterization_sep: the character(s) that seperate method name and method
mikecase (-- gone --) 2017/07/18 23:59:11 Update this variable name to what is actually is.
the real yoland 2017/07/19 00:12:49 Done
+ parameterization postfix.
+ Returns:
+ The test name without parameter postfix as a string.
+ """
+ name = GetTestName(test, sep=sep)
+ return name.split(parameter_postfix)[0]
+
+
def GetUniqueTestName(test, sep='#'):
"""Gets the unique name of the given test.
@@ -768,8 +842,13 @@ class InstrumentationTestInstance(test_instance.TestInstance):
def GetDataDependencies(self):
return self._data_deps
- def GetTests(self):
- if self.test_jar:
+ def GetTests(self, device=None):
+ if self._test_runner_junit4:
+ tests = GetAllTestsFromRunner(
+ device, self.test_apk, self.test_package,
+ test_runner=self._test_runner,
+ test_runner_junit4=self.test_runner_junit4)
+ elif self.test_jar:
tests = GetAllTestsFromJar(self.test_jar)
else:
tests = GetAllTestsFromApk(self.test_apk.path)

Powered by Google App Engine
This is Rietveld 408576698