pytest + xdist没有捕获输出?

And*_*gee 12 python pytest xdist

我正在使用pytest和pytest-xdist进行并行测试运行.-s在运行测试时,似乎没有遵守将标准输出传递到终端的选项.有没有办法让这种情况发生?我意识到这可能会导致不同进程的输出在终端中混乱,但我很好.

Ste*_*rta 9

我找到了一个解决方法,虽然不是一个完整的解决方案。通过将 stdout 重定向到 stderr,可以显示打印语句的输出。这可以通过一行 Python 代码来完成:

sys.stdout = sys.stderr
Run Code Online (Sandbox Code Playgroud)

如果放在 conftest.py 中,它适用于所有测试。


小智 0

我使用了以下代码:

# 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()
Run Code Online (Sandbox Code Playgroud)

将其插入conftest.py

最主要的是要确保它是远程会话,并且我们必须重新配置CaptureManager实例。有一个未解决的问题是如何检查远程对象是否具有“ __channelexec__.SlaveInteractor”类型。