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
--- 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):