pytest-xdist
pytest-xdist copied to clipboard
Individual log files not created for each worker
I am trying to get log file for each worker. I have followed https://github.com/pytest-dev/pytest-xdist/issues/331. I have added code in conftest.py and pytest.ini file, but still separate log files are not generated. Below are the details of my files. @nbartos @nicoddemus @marcosanchotene Could someone please help me out. Also how the value of PYTEST_XDIST_WORKER' is picked up do we need to define it somewhere. I am invoking my tests from commandline through this pytest -v -s -m "smoke or regression" -n 3 --worker-log-file Logs/pytest-%w.log TestCases/
My pytest.ini file
[pytest]
worker_log_file = Logs/pytest-%w.log
log_file_format = [%(asctime)s %(levelname)s %(filename)s %(lineno)s] %(message)s
log_file_level = INFO
My conftest.py file code
import os
from selenium import webdriver
import logging
import pytest
from Utilities.readProperties import ReadConfig
from dotenv import load_dotenv
# Load environment variables from the .env file
load_dotenv()
def pytest_addoption(parser): # This will get the value from Commandline
parser.addoption("--browser")
parser.addoption("--mode")
log_help_text = 'Similar to log_file, but %%w will be replaced with a worker identifier.'
parser.addini('worker_log_file', help=log_help_text)
log_group = parser.getgroup('logging')
log_group.addoption('--worker-log-file', dest='worker_log_file', help=log_help_text)
def pytest_configure(config):
configure_logger(config)
def configure_logger(config):
if is_xdist_enabled():
log_file = config.getini('worker_log_file')
logging.basicConfig(
format=config.getini('log_file_format'),
filename=log_file.replace('%w', os.environ.get('PYTEST_XDIST_WORKER')),
level=config.getini('log_file_level')
)
def is_xdist_enabled():
return os.environ.get('PYTEST_XDIST_WORKER') is not None
PYTEST_XDIST_WORKER is an environment variable that gets set by pytest-xdist for each worker it creates.
Besides that, your configuration works completely fine for me, creating separate log files for each worker (with -n 3, i get a pytest-gw0.log, pytest-gw1.log, and pytest-gw2.log). However, i had to mkdir Logs first, before that pytest was throwing an INTERNALERROR> FileNotFoundError.
Below are my current deps:
$ uv pip list
Package Version
------------ -------
execnet 2.1.1
iniconfig 2.0.0
packaging 24.1
pluggy 1.5.0
pytest 8.3.2
pytest-xdist 3.6.1