Skip to content

Commit

Permalink
Bug 1339178 - Use pytest to run python-tests, r=davehunt
Browse files Browse the repository at this point in the history
This switches most tests over to use pytest as the runner instead of unittest (taking
advantage of the fact that pytest can run unittest based tests).

There were a couple tests that had failures when swithing to pytest:
config/tests/unit-expandlibs.py
xpcom/idl-parser/xpidl/runtests.py

For these tests, I added a runwith='unittest' argument so that they still run the
same way as before. Once we fix them to use pytest, the unittest logic in mozunit.py
can be deleted.

MozReview-Commit-ID: Gcsz6z8MeOi

--HG--
extra : rebase_source : 3c762422ce0af54cbbe7d9fc20085a2d1ebe7057
  • Loading branch information
ahal committed Aug 29, 2017
1 parent 74afe80 commit f79b06a
Show file tree
Hide file tree
Showing 22 changed files with 58 additions and 46 deletions.
19 changes: 18 additions & 1 deletion config/mozunit.py
Expand Up @@ -9,6 +9,8 @@
from StringIO import StringIO
from unittest import TextTestRunner as _TestRunner, TestResult as _TestResult

import pytest

'''Helper to make python unit tests report the way that the Mozilla
unit test infrastructure expects tests to report.
Expand Down Expand Up @@ -209,4 +211,19 @@ def _wrapped_isdir(self, p):


def main(*args, **kwargs):
unittest.main(testRunner=MozTestRunner(), *args, **kwargs)
runwith = kwargs.pop('runwith', 'pytest')

if runwith == 'unittest':
unittest.main(testRunner=MozTestRunner(), *args, **kwargs)
else:
args = list(args)
if os.environ.get('MACH_STDOUT_ISATTY') and not any(a.startswith('--color') for a in args):
args.append('--color=yes')

module = __import__('__main__')
args.extend([
'--verbose',
'-p', 'mozlog.pytest_mozlog.plugin',
module.__file__,
])
sys.exit(pytest.main(args))
2 changes: 1 addition & 1 deletion config/tests/unit-expandlibs.py
Expand Up @@ -428,4 +428,4 @@ def test_getOrderedSectionsWithICF(self):


if __name__ == '__main__':
mozunit.main()
mozunit.main(runwith='unittest')
2 changes: 1 addition & 1 deletion python/mach_commands.py
Expand Up @@ -206,7 +206,7 @@ def _line_handler(line):
file_displayed_test.append(True)

# Hack to make sure treeherder highlights pytest failures
if line.endswith('FAILED'):
if 'FAILED' in line.rsplit(' ', 1)[-1]:
line = line.replace('FAILED', 'TEST-UNEXPECTED-FAIL')

_log(line)
Expand Down
5 changes: 3 additions & 2 deletions python/mozbuild/mozbuild/test/test_pythonutil.py
Expand Up @@ -12,11 +12,12 @@ class TestIterModules(unittest.TestCase):
def test_iter_modules_in_path(self):
mozbuild_path = os.path.normcase(os.path.dirname(os.path.dirname(__file__)))
paths = list(iter_modules_in_path(mozbuild_path))
self.assertEquals(sorted(paths), [
self.assertEquals(set(paths), set([
os.path.join(os.path.abspath(mozbuild_path), '__init__.py'),
os.path.join(os.path.abspath(mozbuild_path), 'pythonutil.py'),
os.path.join(os.path.abspath(mozbuild_path), 'test', '__init__.py'),
os.path.join(os.path.abspath(mozbuild_path), 'test', 'test_pythonutil.py'),
])
]))


if __name__ == '__main__':
Expand Down
4 changes: 2 additions & 2 deletions python/mozlint/test/test_cli.py
Expand Up @@ -3,9 +3,9 @@
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

import os
import sys
from distutils.spawn import find_executable

import mozunit
import pytest

from mozlint import cli
Expand Down Expand Up @@ -58,4 +58,4 @@ def test_cli_run_with_edit(run, parser, capfd):


if __name__ == '__main__':
sys.exit(pytest.main(['--verbose', __file__]))
mozunit.main()
4 changes: 2 additions & 2 deletions python/mozlint/test/test_formatters.py
Expand Up @@ -5,9 +5,9 @@
from __future__ import unicode_literals

import json
import sys
from collections import defaultdict

import mozunit
import pytest

from mozlint import ResultContainer
Expand Down Expand Up @@ -104,4 +104,4 @@ def test_json_formatter(results):


if __name__ == '__main__':
sys.exit(pytest.main(['--verbose', __file__]))
mozunit.main()
4 changes: 2 additions & 2 deletions python/mozlint/test/test_parser.py
Expand Up @@ -3,8 +3,8 @@
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

import os
import sys

import mozunit
import pytest

from mozlint.parser import Parser
Expand Down Expand Up @@ -58,4 +58,4 @@ def test_parse_non_existent_linter(parse):


if __name__ == '__main__':
sys.exit(pytest.main(['--verbose', __file__]))
mozunit.main()
3 changes: 2 additions & 1 deletion python/mozlint/test/test_roller.py
Expand Up @@ -5,6 +5,7 @@
import os
import sys

import mozunit
import pytest

from mozlint import ResultContainer
Expand Down Expand Up @@ -79,4 +80,4 @@ def test_roll_with_failure_code(lint, lintdir, files):


if __name__ == '__main__':
sys.exit(pytest.main(['--verbose', __file__]))
mozunit.main()
4 changes: 2 additions & 2 deletions python/mozlint/test/test_types.py
Expand Up @@ -3,8 +3,8 @@
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

import os
import sys

import mozunit
import pytest

from mozlint.result import ResultContainer
Expand Down Expand Up @@ -51,4 +51,4 @@ def test_no_filter(lint, lintdir, files):


if __name__ == '__main__':
sys.exit(pytest.main(['--verbose', __file__]))
mozunit.main()
4 changes: 2 additions & 2 deletions python/mozlint/test/test_vcs.py
Expand Up @@ -4,8 +4,8 @@

import os
import subprocess
import sys

import mozunit
import pytest

from mozlint.vcs import VCSHelper, vcs_class
Expand Down Expand Up @@ -119,4 +119,4 @@ def test_vcs_helper(repo):


if __name__ == '__main__':
sys.exit(pytest.main(['--verbose', __file__]))
mozunit.main()
@@ -1,7 +1,2 @@
[pytest]
# Early-load pytest_mozlog plugin to replace terminal reporter.
# Adding pytest_mozlog plugin to conftest.py registers the plugin
# too late for tests to recognize mozlog options.
# This manual registration of plugin is needed for running these
# tests in mach, whose virtualenv setup does not call mozlog's setup.py
addopts = -p mozlog.pytest_mozlog.plugin -p no:terminalreporter
addopts = -p no:terminalreporter
Expand Up @@ -7,6 +7,7 @@
import types
import urllib2

import mozunit
import pytest

from wptserve.handlers import json_handler
Expand Down Expand Up @@ -86,6 +87,4 @@ def handler(request, response):


if __name__ == "__main__":
import sys
sys.exit(pytest.main(
['--log-tbpl=-', __file__]))
mozunit.main('--log-tbpl=-')
@@ -1,6 +1,7 @@
# 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/.
import mozunit
import pytest

from marionette_harness.runtests import MarionetteArguments
Expand Down Expand Up @@ -28,6 +29,4 @@ def _is_float_convertible(value):


if __name__ == '__main__':
import sys
sys.exit(pytest.main(
['--log-tbpl=-', __file__]))
mozunit.main('--log-tbpl=-')
Expand Up @@ -2,6 +2,7 @@
# 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/.

import mozunit
import pytest

from mock import Mock, patch, sentinel
Expand Down Expand Up @@ -104,6 +105,4 @@ def test_harness_sets_up_default_test_handlers(mach_parsed_kwargs):


if __name__ == '__main__':
import sys
sys.exit(pytest.main(
['--log-tbpl=-', __file__]))
mozunit.main('--log-tbpl=-')
Expand Up @@ -3,6 +3,7 @@
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

import manifestparser
import mozunit
import pytest

from mock import Mock, patch, mock_open, sentinel, DEFAULT
Expand Down Expand Up @@ -507,6 +508,4 @@ def test_option_run_until_failure(mach_parsed_kwargs, repeat, run_until_failure)


if __name__ == '__main__':
import sys
sys.exit(pytest.main(
['--log-tbpl=-', __file__]))
mozunit.main('--log-tbpl=-')
Expand Up @@ -2,6 +2,7 @@
# 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/.

import mozunit
import pytest

from marionette_harness import MarionetteTestResult
Expand Down Expand Up @@ -50,6 +51,4 @@ def test_crash_is_recorded_as_error(empty_marionette_test,


if __name__ == '__main__':
import sys
sys.exit(pytest.main(
['--log-tbpl=-', __file__]))
mozunit.main('--log-tbpl=-')
Expand Up @@ -4,6 +4,7 @@

import types

import mozunit
import pytest

from marionette_harness.runner import serve
Expand Down Expand Up @@ -63,6 +64,4 @@ def test_where_is():


if __name__ == "__main__":
import sys
sys.exit(pytest.main(
['--log-tbpl=-', __file__]))
mozunit.main('--log-tbpl=-')
3 changes: 2 additions & 1 deletion testing/mochitest/tests/python/test_basic_mochitest_plain.py
Expand Up @@ -6,6 +6,7 @@
import os
import sys

import mozunit
import pytest

from conftest import build, filter_action
Expand Down Expand Up @@ -162,4 +163,4 @@ def process_leak_log(*args, **kwargs):


if __name__ == '__main__':
sys.exit(pytest.main(['--verbose', __file__]))
mozunit.main()
4 changes: 2 additions & 2 deletions testing/mochitest/tests/python/test_get_active_tests.py
Expand Up @@ -5,11 +5,11 @@
from __future__ import print_function, unicode_literals

import os
import sys
from argparse import Namespace

from manifestparser import TestManifest

import mozunit
import pytest


Expand Down Expand Up @@ -88,4 +88,4 @@ def test_prefs_validation(get_active_tests, create_manifest):


if __name__ == '__main__':
sys.exit(pytest.main(['--verbose', __file__]))
mozunit.main()
5 changes: 4 additions & 1 deletion testing/mozbase/mozcrash/tests/test.py
Expand Up @@ -20,7 +20,10 @@
import mozlog.unstructured as mozlog

# Make logs go away
log = mozlog.getLogger("mozcrash", handler=mozlog.FileHandler(os.devnull))
try:
log = mozlog.getLogger("mozcrash", handler=mozlog.FileHandler(os.devnull))
except ValueError:
pass


def popen_factory(stdouts):
Expand Down
2 changes: 1 addition & 1 deletion testing/mozbase/mozlog/tests/test_logger.py
Expand Up @@ -233,7 +233,7 @@ def test_mixin(self):
self.assertTrue(not hasattr(loggable, "_logger"))
loggable.log(mozlog.INFO, "This will instantiate the logger")
self.assertTrue(hasattr(loggable, "_logger"))
self.assertEqual(loggable._logger.name, "__main__.Loggable")
self.assertEqual(loggable._logger.name, "test_logger.Loggable")

self.assertRaises(ValueError, loggable.set_logger,
"not a logger")
Expand Down
2 changes: 1 addition & 1 deletion xpcom/idl-parser/xpidl/runtests.py
Expand Up @@ -110,4 +110,4 @@ def write(self, s):
self.assertEqual(e.args[0], "Unexpected overloaded virtual method GetBar in interface foo")

if __name__ == '__main__':
mozunit.main()
mozunit.main(runwith='unittest')

0 comments on commit f79b06a

Please sign in to comment.