Bug 1285299 - Report collected item count in pytest-mozlog; r?maja_zf
Add/modify Pytest hooks in the pytest-mozlog plugin so that the
suite_start event sent to the logger contains the correct list
of tests to be run, i.e. items collected by Pytest, instead
of an empty list. This allows mozlog to log the correct number
of tests to be run, instead of always loggin "Running 0 tests".
Instead of logging the suite_start event in the pytest_sessionstart
hook, use that hook to simply record the session start time.
Add the pytest_collection_modifyitems hook, which is executed
after tests are collected and before they are run, and log
the suite_start event in that hook, passing in the start time
and the list of tests.
MozReview-Commit-ID: L2tY8roaeDw
--- a/testing/mozbase/mozlog/mozlog/pytest_mozlog/plugin.py
+++ b/testing/mozbase/mozlog/mozlog/pytest_mozlog/plugin.py
@@ -1,14 +1,15 @@
# 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 mozlog
import os
+import time
def pytest_addoption(parser):
group = parser.getgroup('mozlog')
for name, (_class, _help) in mozlog.commandline.log_formatters.iteritems():
group.addoption('--log-{0}'.format(name), action='append', help=_help)
formatter_options = mozlog.commandline.fmt_options.iteritems()
@@ -25,16 +26,17 @@ def pytest_configure(config):
if not hasattr(config, 'slaveinput'):
config.pluginmanager.register(MozLog())
class MozLog(object):
def __init__(self):
self.results = {}
+ self.start_time = 0
def format_nodeid(self, nodeid):
'''Helper to Reformat/shorten a "::"-separated pytest test nodeid'''
testfile, testname = nodeid.split("::")
return " ".join([os.path.basename(testfile), testname])
def pytest_configure(self, config):
args = {}
@@ -52,18 +54,26 @@ class MozLog(object):
if config.getoption(argname):
args[argname] = config.getoption(argname)
# Prevent mozlog from logging to stdout by default with `defaults={}`
# (User can specify stdout logger using `--log-* -` option)
mozlog.commandline.setup_logging('pytest', args, defaults={})
self.logger = mozlog.get_default_logger(component='pytest')
+
def pytest_sessionstart(self, session):
- self.logger.suite_start([])
+ # we can't log suite_start because Pytest hasn't collected tests yet,
+ # so we just record the start time (in ms like mozlog does)
+ # and wait to log suite_start in pytest_collection_modifyitems
+ self.start_time = int(time.time() * 1000)
+
+ def pytest_collection_modifyitems(self, items):
+ # called after test collection is completed, just before tests are run
+ self.logger.suite_start(tests=items, time=self.start_time)
def pytest_sessionfinish(self, session, exitstatus):
self.logger.suite_end()
def pytest_runtest_logstart(self, nodeid, location):
self.logger.test_start(test=self.format_nodeid(nodeid))
def pytest_runtest_logreport(self, report):