Bug 1285299 - Report collected item count in pytest-mozlog; r?maja_zf draft
authorAnjana Vakil <anjanavakil@gmail.com>
Mon, 05 Sep 2016 15:28:00 +0200
changeset 409916 804efdedb27d064d800cb74f9c887e5b694241ad
parent 409915 40d39ad7793de3837b06824e2499e36889e4258c
child 409917 2ff74d8f7041d8f8ac1c1149d136337ae69bb057
push id28611
push userbmo:anjanavakil@gmail.com
push dateMon, 05 Sep 2016 17:25:40 +0000
reviewersmaja_zf
bugs1285299
milestone51.0a1
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
testing/mozbase/mozlog/mozlog/pytest_mozlog/plugin.py
--- 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):