Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Individual log files not created for each worker #1010

Open
QAingP opened this issue Jan 25, 2024 · 0 comments
Open

Individual log files not created for each worker #1010

QAingP opened this issue Jan 25, 2024 · 0 comments

Comments

@QAingP
Copy link

QAingP commented Jan 25, 2024

I am trying to get log file for each worker. I have followed #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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant