2

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
Vadim Kovrizhkin
  • 1,575
  • 4
  • 16
  • 27

1 Answers1

2

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.

hoefling
  • 59,418
  • 12
  • 147
  • 194
  • But I use log messages not only in tests. So how to get 'gw' from test I know, but how to set it globally for logging - I don't (is it possible) – Vadim Kovrizhkin Feb 23 '18 at 12:08
  • 1
    So you don't know how to access the `config` outside of tests? `import pytest; config = pytest.config; logging.info(config.slaveinput['slaveid'])`. – hoefling Feb 23 '18 at 13:30
  • @hoefling theres no such thing as pytest.config I am using 3.7.3 – Imran Jul 24 '20 at 14:54
  • 1
    @Imran the answer is 2.5 years old; since then, `pytest.config` was deprecated and removed. – hoefling Jul 24 '20 at 14:57
  • yes. Can't find anyway to access pytest config outside of the test – Imran Jul 24 '20 at 15:00
  • @Imran there's no way of accessing config on module level anymore. Move the code to a fixture or a hookimpl that has the access to config object. – hoefling Jul 24 '20 at 16:07