Bug 780329 - Part 6: Add testing modules to mozbuild; r=jhammel

This commit is contained in:
Gregory Szorc 2012-09-26 09:43:53 -07:00
parent 46f304b750
commit 37b216c67f
5 changed files with 223 additions and 0 deletions

View File

@ -12,6 +12,7 @@ Modules Overview
includes managing compiler warnings.
* mozbuild.logging -- Defines mozbuild's logging infrastructure.
mozbuild uses a structured logging backend.
* mozbuild.testing -- Interfaces for running tests.
Structured Logging
==================

View File

@ -0,0 +1,82 @@
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this file,
# You can obtain one at http://mozilla.org/MPL/2.0/.
from __future__ import unicode_literals
import os
from mozbuild.base import MozbuildObject
class MochitestRunner(MozbuildObject):
"""Easily run mochitests.
This currently contains just the basics for running mochitests. We may want
to hook up result parsing, etc.
"""
def run_plain_suite(self):
"""Runs all plain mochitests."""
# TODO hook up Python harness runner.
self._run_make(directory='.', target='mochitest-plain')
def run_chrome_suite(self):
"""Runs all chrome mochitests."""
# TODO hook up Python harness runner.
self._run_make(directory='.', target='mochitest-chrome')
def run_browser_chrome_suite(self):
"""Runs browser chrome mochitests."""
# TODO hook up Python harness runner.
self._run_make(directory='.', target='mochitest-browser-chrome')
def run_all(self):
self.run_plain_suite()
self.run_chrome_suite()
self.run_browser_chrome_suite()
def run_mochitest_test(self, test_file=None, suite=None):
"""Runs a mochitest.
test_file is a path to a test file. It can be a relative path from the
top source directory, an absolute filename, or a directory containing
test files.
suite is the type of mochitest to run. It can be one of ('plain',
'chrome', 'browser').
"""
if test_file is None:
raise Exception('test_file must be defined.')
parsed = self._parse_test_path(test_file)
# TODO hook up harness via native Python
target = None
if suite == 'plain':
target = 'mochitest-plain'
elif suite == 'chrome':
target = 'mochitest-chrome'
elif suite == 'browser':
target = 'mochitest-browser-chrome'
else:
raise Exception('None or unrecognized mochitest suite type.')
env = {'TEST_PATH': parsed['normalized']}
self._run_make(directory='.', target=target, env=env)
def _parse_test_path(self, test_path):
is_dir = os.path.isdir(test_path)
if is_dir and not test_path.endswith(os.path.sep):
test_path += os.path.sep
normalized = test_path
if test_path.startswith(self.topsrcdir):
normalized = test_path[len(self.topsrcdir):]
return {
'normalized': normalized,
'is_dir': is_dir,
}

View File

@ -0,0 +1,49 @@
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this file,
# You can obtain one at http://mozilla.org/MPL/2.0/.
from __future__ import unicode_literals
from mozbuild.base import MozbuildObject
from mozbuild.testing.xpcshell import XPCShellRunner
from mozbuild.testing.mochitest import MochitestRunner
class Suite(MozbuildObject):
def run_suite(self, suite):
"""Run a named test suite.
Recognized names are:
all - All test suites
mochitest-plain - Plain mochitests
mochitest-chrome - mochitests with chrome
mochitest-browser - mochitests with browser chrome
xpcshell - xpcshell tests
TODO support for other test suite types.
"""
xpcshell = self._spawn(XPCShellRunner)
mochitest = self._spawn(MochitestRunner)
if suite == 'all':
xpcshell.run_suite()
mochitest.run_plain_suite()
mochitest.run_chrome_suite()
mochitest.run_browser_chrome_suite()
return
m = {
'xpcshell': xpcshell.run_suite,
'mochitest-plain': mochitest.run_plain_suite,
'mochitest-chrome': mochitest.run_chrome_suite,
'mochitest-browser': mochitest.run_browser_chrome_suite,
}
method = m.get(suite, None)
if method is None:
raise Exception('Unknown test suite: %s' % suite)
return method()

View File

@ -0,0 +1,91 @@
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this file,
# You can obtain one at http://mozilla.org/MPL/2.0/.
# This modules contains code for interacting with xpcshell tests.
from __future__ import unicode_literals
import os.path
from StringIO import StringIO
from mozbuild.base import MozbuildObject
class XPCShellRunner(MozbuildObject):
"""Run xpcshell tests."""
def run_suite(self):
# TODO hook up to harness runner and support things like shuffle,
# proper progress updates, etc.
self._run_make(directory='.', target='xpcshell-tests')
def run_test(self, test_file, debug=False):
"""Runs an individual xpcshell test."""
if test_file == 'all':
self.run_suite()
return
# dirname() gets confused if there isn't a trailing slash.
if os.path.isdir(test_file) and not test_file.endswith(os.path.sep):
test_file += os.path.sep
relative_dir = test_file
if test_file.startswith(self.topsrcdir):
relative_dir = test_file[len(self.topsrcdir):]
test_dir = os.path.join(self.topobjdir, '_tests', 'xpcshell',
os.path.dirname(relative_dir))
args = {
'debug': debug,
'test_dirs': [test_dir],
}
if os.path.isfile(test_file):
args['test_path'] = os.path.basename(test_file)
self._run_xpcshell_harness(**args)
def _run_xpcshell_harness(self, test_dirs=None, manifest=None,
test_path=None, debug=False):
# Obtain a reference to the xpcshell test runner.
import runxpcshelltests
dummy_log = StringIO()
xpcshell = runxpcshelltests.XPCShellTests(log=dummy_log)
self.log_manager.enable_unstructured()
tests_dir = os.path.join(self.topobjdir, '_tests', 'xpcshell')
modules_dir = os.path.join(self.topobjdir, '_tests', 'modules')
args = {
'xpcshell': os.path.join(self.bindir, 'xpcshell'),
'mozInfo': os.path.join(self.topobjdir, 'mozinfo.json'),
'symbolsPath': os.path.join(self.distdir, 'crashreporter-symbols'),
'logfiles': False,
'testsRootDir': tests_dir,
'testingModulesDir': modules_dir,
'profileName': 'firefox',
'verbose': test_path is not None,
}
if manifest is not None:
args['manifest'] = manifest
elif test_dirs is not None:
if isinstance(test_dirs, list):
args['testdirs'] = test_dirs
else:
args['testdirs'] = [test_dirs]
else:
raise Exception('One of test_dirs or manifest must be provided.')
if test_path is not None:
args['testPath'] = test_path
# TODO do something with result.
xpcshell.runTests(**args)
self.log_manager.disable_unstructured()