Bug 1339178 - Use pytest to run python-tests, r=davehunt

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
This commit is contained in:
Andrew Halberstadt 2017-08-29 14:50:33 -04:00
parent 74afe80557
commit f79b06a32a
22 changed files with 58 additions and 46 deletions

View File

@ -9,6 +9,8 @@ import unittest
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.
@ -209,4 +211,19 @@ class MockedOpen(object):
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))

View File

@ -428,4 +428,4 @@ class TestSymbolOrder(unittest.TestCase):
if __name__ == '__main__':
mozunit.main()
mozunit.main(runwith='unittest')

View File

@ -206,7 +206,7 @@ class MachCommands(MachCommandBase):
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)

View File

@ -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__':

View File

@ -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
@ -58,4 +58,4 @@ def test_cli_run_with_edit(run, parser, capfd):
if __name__ == '__main__':
sys.exit(pytest.main(['--verbose', __file__]))
mozunit.main()

View File

@ -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
@ -104,4 +104,4 @@ def test_json_formatter(results):
if __name__ == '__main__':
sys.exit(pytest.main(['--verbose', __file__]))
mozunit.main()

View File

@ -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
@ -58,4 +58,4 @@ def test_parse_non_existent_linter(parse):
if __name__ == '__main__':
sys.exit(pytest.main(['--verbose', __file__]))
mozunit.main()

View File

@ -5,6 +5,7 @@
import os
import sys
import mozunit
import pytest
from mozlint import ResultContainer
@ -79,4 +80,4 @@ def test_roll_with_failure_code(lint, lintdir, files):
if __name__ == '__main__':
sys.exit(pytest.main(['--verbose', __file__]))
mozunit.main()

View File

@ -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
@ -51,4 +51,4 @@ def test_no_filter(lint, lintdir, files):
if __name__ == '__main__':
sys.exit(pytest.main(['--verbose', __file__]))
mozunit.main()

View File

@ -4,8 +4,8 @@
import os
import subprocess
import sys
import mozunit
import pytest
from mozlint.vcs import VCSHelper, vcs_class
@ -119,4 +119,4 @@ def test_vcs_helper(repo):
if __name__ == '__main__':
sys.exit(pytest.main(['--verbose', __file__]))
mozunit.main()

View File

@ -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

View File

@ -7,6 +7,7 @@ import os
import types
import urllib2
import mozunit
import pytest
from wptserve.handlers import json_handler
@ -86,6 +87,4 @@ def test_handler(server):
if __name__ == "__main__":
import sys
sys.exit(pytest.main(
['--log-tbpl=-', __file__]))
mozunit.main('--log-tbpl=-')

View File

@ -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
@ -28,6 +29,4 @@ def test_parse_arg_socket_timeout(socket_timeout):
if __name__ == '__main__':
import sys
sys.exit(pytest.main(
['--log-tbpl=-', __file__]))
mozunit.main('--log-tbpl=-')

View File

@ -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
@ -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=-')

View File

@ -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
@ -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=-')

View File

@ -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
@ -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=-')

View File

@ -4,6 +4,7 @@
import types
import mozunit
import pytest
from marionette_harness.runner import serve
@ -63,6 +64,4 @@ def test_where_is():
if __name__ == "__main__":
import sys
sys.exit(pytest.main(
['--log-tbpl=-', __file__]))
mozunit.main('--log-tbpl=-')

View File

@ -6,6 +6,7 @@ import json
import os
import sys
import mozunit
import pytest
from conftest import build, filter_action
@ -162,4 +163,4 @@ def test_output_leak(monkeypatch, runtests):
if __name__ == '__main__':
sys.exit(pytest.main(['--verbose', __file__]))
mozunit.main()

View File

@ -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
@ -88,4 +88,4 @@ prefs=foo=bar
if __name__ == '__main__':
sys.exit(pytest.main(['--verbose', __file__]))
mozunit.main()

View File

@ -20,7 +20,10 @@ import mozhttpd
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):

View File

@ -233,7 +233,7 @@ class TestLoggingMixin(unittest.TestCase):
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")

View File

@ -110,4 +110,4 @@ void getBar();
self.assertEqual(e.args[0], "Unexpected overloaded virtual method GetBar in interface foo")
if __name__ == '__main__':
mozunit.main()
mozunit.main(runwith='unittest')