Bug 1338531 - [mozlog] Collect tests when using pytest-xdist and send suite_start when the first test starts. r?ahal draft
authorDave Hunt <dhunt@mozilla.com>
Mon, 13 Feb 2017 18:46:10 +0000
changeset 483638 e2d96e3fe69c266dc20ed56d15ac2de75f431d7b
parent 483637 47391e531350873bfccd576b689259ec249aede8
child 545676 d6baaae5e1a565f102ef35419a3c41298601b9ab
push id45352
push userdhunt@mozilla.com
push dateTue, 14 Feb 2017 15:52:42 +0000
reviewersahal
bugs1338531
milestone54.0a1
Bug 1338531 - [mozlog] Collect tests when using pytest-xdist and send suite_start when the first test starts. r?ahal When using pytest-xdist we need to gather the tests for each slave. Lack of a better hook means I'm then using the first test start to log the suite_start message including the collected tests. MozReview-Commit-ID: 7l22z9RvIhx
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
@@ -28,31 +28,43 @@ def pytest_configure(config):
     # If using pytest-xdist for parallelization, only register plugin on master process
     if not hasattr(config, 'slaveinput'):
         config.pluginmanager.register(MozLog())
 
 
 class MozLog(object):
 
     def __init__(self):
+        self._started = False
         self.results = {}
         self.start_time = int(time.time() * 1000)  # in ms for Mozlog compatibility
 
+    def _log_suite_start(self, tests):
+        if not self._started:
+            # As this is called for each node when using pytest-xdist, we want
+            # to avoid logging multiple suite_start messages.
+            self.logger.suite_start(tests=tests, time=self.start_time)
+            self._started = True
+
     def pytest_configure(self, config):
         mozlog.commandline.setup_logging('pytest', config.known_args_namespace,
                                          defaults={}, allow_unused_options=True)
         self.logger = mozlog.get_default_logger(component='pytest')
 
     def pytest_sessionstart(self, session):
         '''Called before test collection; records suite start time to log later'''
         self.start_time = int(time.time() * 1000)  # in ms for Mozlog compatibility
 
-    def pytest_collection_modifyitems(self, items):
+    def pytest_collection_finish(self, session):
         '''Called after test collection is completed, just before tests are run (suite start)'''
-        self.logger.suite_start(tests=items, time=self.start_time)
+        self._log_suite_start([item.nodeid for item in session.items])
+
+    def pytest_xdist_node_collection_finished(self, node, ids):
+        '''Called after each pytest-xdist node collection is completed'''
+        self._log_suite_start(ids)
 
     def pytest_sessionfinish(self, session, exitstatus):
         self.logger.suite_end()
 
     def pytest_runtest_logstart(self, nodeid, location):
         self.logger.test_start(test=nodeid)
 
     def pytest_runtest_logreport(self, report):