18
votes

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.

2
There's an open issue for this here: github.com/pytest-dev/pytest/issues/680Chris Clark
woohoo, pytest xdist now honors the -s option! See above issue.Chris Clark

2 Answers

9
votes

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.

0
votes

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.