Why does pytest + xdist not capture output?

5.2k views Asked by At

I'm using pytest with pytest-xdist for parallel test running. It doesn't seem to honour the -s option for passing through the standard output to the terminal as the tests are run. Is there any way to make this happen? I realise this could cause the output from the different processes to be jumbled up in the terminal but I'm ok with that.

3

There are 3 answers

5
Steve Saporta On

I found a workaround, although not a full solution. By redirecting stdout to stderr, the output of print statements is displayed. This can be accomplished with a single line of Python code:

sys.stdout = sys.stderr

If placed in conftest.py, it applies to all tests.

1
xSus On

I used the followed code:

# conftest.py
import _pytest.capture

def get_capman(plugin_manager):
    capman_list = filter(lambda p: isinstance(p, _pytest.capture.CaptureManager), plugin_manager._plugins)
    return capman_list[0] if len(capman_list) == 1 else None


def get_xdist_slave(plugin_manager):
    # TODO: have no idea how to check isinstance "__channelexec__.SlaveInteractor"
    slave_list = filter(lambda p: hasattr(p, 'slaveid'), plugin_manager._plugins)
    return slave_list[0] if len(slave_list) == 1 else None


def is_remote_xdist_session(plugin_manager):
    return get_xdist_slave(plugin_manager) is not None


def pytest_configure(config):
    if is_remote_xdist_session(config.pluginmanager) and get_capman(config.pluginmanager) is not None:
        capman = get_capman(config.pluginmanager)
        capman._method = "no"
        capman.reset_capturings()
        capman.init_capturings()

Insert it to conftest.py

The main thing is to be sure that it is remote session, and we have to reconfigure CaptureManager instance. There one unresolved issue is how to check that remote object has "__channelexec__.SlaveInteractor" type.

0
Samuel Dauzon On

Steve's solution is great but sometimes we can't modifiy an conftest.py shared file.

I use -s argument on pytest command :

pytest -s path/test_file.py

Inside my Python code I use these lines :

import sys
print("\nMy DEBUG line", file=sys.stderr)

pytest prompt me my DEBUG line. You can improve this solution according to your needs.