Commit 9a6b7b33 authored by jbudorick's avatar jbudorick Committed by Commit bot

[Android] JUnit test runner changes.

BUG=316383

Review URL: https://codereview.chromium.org/566643002

Cr-Commit-Position: refs/heads/master@{#295819}
parent f54ff4dd
# Copyright 2014 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
# Copyright 2014 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
from pylib.junit import test_runner
def Setup(options):
"""Creates a test runner factory for junit tests.
Return:
A (runner_factory, tests) tuple.
"""
def TestRunnerFactory(_unused_device, _unused_shard_index):
return test_runner.JavaTestRunner(options)
return (TestRunnerFactory, ['JUnit tests'])
# Copyright 2014 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
def RunTests(tests, runner_factory):
"""Runs a set of java tests on the host.
Return:
A tuple containing the results & the exit code.
"""
def run(t):
runner = runner_factory(None, None)
runner.SetUp()
result = runner.RunTest(t)
runner.TearDown()
return result == 0
return (None, 0 if all(run(t) for t in tests) else 1)
# Copyright 2014 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
import os
from pylib import cmd_helper
from pylib import constants
class JavaTestRunner(object):
"""Runs java tests on the host."""
def __init__(self, options):
self._package_filter = options.package_filter
self._runner_filter = options.runner_filter
self._sdk_version = options.sdk_version
self._test_filter = options.test_filter
self._test_suite = options.test_suite
def SetUp(self):
pass
def RunTest(self, _test):
"""Runs junit tests from |self._test_suite|."""
command = ['java',
'-jar', os.path.join(constants.GetOutDirectory(), 'lib.java',
'%s.jar' % self._test_suite)]
if self._test_filter:
command.extend(['-gtest-filter', self._test_filter])
if self._package_filter:
command.extend(['-package-filter', self._package_filter])
if self._runner_filter:
command.extend(['-runner-filter', self._runner_filter])
if self._sdk_version:
command.extend(['-sdk-version', self._sdk_version])
return cmd_helper.RunCmd(command)
def TearDown(self):
pass
......@@ -28,6 +28,8 @@ from pylib.linker import setup as linker_setup
from pylib.host_driven import setup as host_driven_setup
from pylib.instrumentation import setup as instrumentation_setup
from pylib.instrumentation import test_options as instrumentation_test_options
from pylib.junit import setup as junit_setup
from pylib.junit import test_dispatcher as junit_dispatcher
from pylib.monkey import setup as monkey_setup
from pylib.monkey import test_options as monkey_test_options
from pylib.perf import setup as perf_setup
......@@ -379,6 +381,36 @@ def ProcessUIAutomatorOptions(options, error_func):
options.package)
def AddJUnitTestOptions(option_parser):
"""Adds junit test options to |option_parser|."""
option_parser.usage = '%prog junit -s [test suite name]'
option_parser.commands_dict = {}
option_parser.add_option(
'-s', '--test-suite', dest='test_suite',
help=('JUnit test suite to run.'))
option_parser.add_option(
'-f', '--test-filter', dest='test_filter',
help='Filters tests googletest-style.')
option_parser.add_option(
'--package-filter', dest='package_filter',
help='Filters tests by package.')
option_parser.add_option(
'--runner-filter', dest='runner_filter',
help='Filters tests by runner class. Must be fully qualified.')
option_parser.add_option(
'--sdk-version', dest='sdk_version', type="int",
help='The Android SDK version.')
AddCommonOptions(option_parser)
def ProcessJUnitTestOptions(options, error_func):
"""Processes all JUnit test options."""
if not options.test_suite:
error_func('No test suite specified.')
return options
def AddMonkeyTestOptions(option_parser):
"""Adds monkey test options to |option_parser|."""
......@@ -635,6 +667,15 @@ def _RunUIAutomatorTests(options, error_func, devices):
return exit_code
def _RunJUnitTests(options, error_func):
"""Subcommand of RunTestsCommand which runs junit tests."""
junit_options = ProcessJUnitTestOptions(options, error_func)
runner_factory, tests = junit_setup.Setup(junit_options)
_, exit_code = junit_dispatcher.RunTests(tests, runner_factory)
return exit_code
def _RunMonkeyTests(options, error_func, devices):
"""Subcommand of RunTestsCommands which runs monkey tests."""
monkey_options = ProcessMonkeyTestOptions(options, error_func)
......@@ -758,6 +799,8 @@ def RunTestsCommand(command, options, args, option_parser):
return _RunInstrumentationTests(options, option_parser.error, devices)
elif command == 'uiautomator':
return _RunUIAutomatorTests(options, option_parser.error, devices)
elif command == 'junit':
return _RunJUnitTests(options, option_parser.error)
elif command == 'monkey':
return _RunMonkeyTests(options, option_parser.error, devices)
elif command == 'perf':
......@@ -818,6 +861,8 @@ VALID_COMMANDS = {
AddInstrumentationTestOptions, RunTestsCommand),
'uiautomator': CommandFunctionTuple(
AddUIAutomatorTestOptions, RunTestsCommand),
'junit': CommandFunctionTuple(
AddJUnitTestOptions, RunTestsCommand),
'monkey': CommandFunctionTuple(
AddMonkeyTestOptions, RunTestsCommand),
'perf': CommandFunctionTuple(
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment