Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to find out which pytest-xdist gateway is running?

I would like to create a separate log file for each subprocess/gateway that is spawned by pytest-xdist. Is there an elegant way of finding out in which subprocess/gateway pytest is currently in? I'm configuring my root logger with a session scoped fixture located in conftest.py, something like this:

@pytest.fixture(scope='session', autouse=True)
def setup_logging():
    logger = logging.getLogger(__name__)
    logger.setLevel(logging.INFO)

    fh = logging.FileHandler('xdist.log')
    fh.setLevel(logging.INFO)

   formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
   fh.setFormatter(formatter)

   logger.addHandler(fh)

It would be great if i could add a prefix to the log file name based on the gateway number, e.g:

 fh = logging.FileHandler('xdist_gateway_%s.log' % gateway_number)

Without this each gateway will use the same log and the logs will get messy. I know that I can add a time stamp to the filename. But this doesn't let me to distinguish quickly which file is from which gateway.

like image 681
Kanguros Avatar asked Jun 24 '14 19:06

Kanguros


1 Answers

I found out that you can access the gateway id in the following way:

slaveinput = getattr(session.config, "slaveinput", None)

if slaveinput:
    gatewayid = slaveinput['slaveid']

Of course you need to be in a place where you can access the session.config object.

like image 115
Kanguros Avatar answered Oct 16 '22 06:10

Kanguros