pythonpytestxdist

Is it possible to print xdist gateway number to each line in stdout?


I am using logging module to print to stdout. It is hard to understand what message corresponds to worker when I run tests with pytest-xdist

Is it possible to print xdist gateway number to each line in stdout?

Example of log messages that I have now:

[02-22-2018_19.44.11] [DEBUG] [file.py:96] - Message
[02-22-2018_19.44.11] [DEBUG] [file2.py:16] - Message
[02-22-2018_19.44.12] [DEBUG] [file3.py:23] - Message
[02-22-2018_19.44.12] [DEBUG] [file4.py:30] - Message

Desired:

[02-22-2018_19.44.11] [gw1] [DEBUG] [file.py:96] - Message
[02-22-2018_19.44.11] [gw0] [DEBUG] [file2.py:16] - Message
[02-22-2018_19.44.12] [gw0] [DEBUG] [file3.py:23] - Message
[02-22-2018_19.44.12] [gw3] [DEBUG] [file4.py:30] - Message

or

gw1 [02-22-2018_19.44.11] [DEBUG] [file.py:96] - Message
gw0 [02-22-2018_19.44.11] [DEBUG] [file2.py:16] - Message
gw0 [02-22-2018_19.44.12] [DEBUG] [file3.py:23] - Message
gw3 [02-22-2018_19.44.12] [DEBUG] [file4.py:30] - Message

Solution

  • You can access the gateway id via the config.slaveinput attribute. Example:

    def test_spam(pytestconfig):
        assert hasattr(pytestconfig, 'slaveinput')
        assert pytestconfig.slaveinput['slaveid'] == 'gw0'
    

    The test will only pass when xdist is actually invoked, e.g. via pytest -n1 test_spam.py, otherwise the slaveinput attribute won't be set.