【问题标题】:Why does pytest + xdist not capture output?为什么 pytest + xdist 不捕获输出?
【发布时间】:2015-01-16 09:11:56
【问题描述】:

我正在使用 pytest 和 pytest-xdist 进行并行测试运行。在运行测试时,它似乎不支持将标准输出传递到终端的 -s 选项。有没有办法做到这一点?我意识到这可能会导致终端中不同进程的输出混乱,但我可以接受。

【问题讨论】:

标签: python pytest xdist pytest-xdist


【解决方案1】:

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

sys.stdout = sys.stderr

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

【讨论】:

  • 这不起作用。执行此操作后,print 的输出仍然不会出现在 xdistworkers 中执行的测试中。
  • @ely 你在使用 log-cli-level=debug-s 标志来运行 pytest 吗?
  • @steve-saporta 出色的解决方法。完美地为我工作。谢谢。
  • 我使用的是-s,但不是log-cli-level - 感谢您回过头来查看!
【解决方案2】:

我使用了以下代码:

# 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()

将其插入到 conftest.py

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

【讨论】:

  • 嗯,这似乎对我不起作用。我的夹具中有print 语句,这些语句肯定正在执行,但我在测试运行中看不到它们的输出。
猜你喜欢
  • 2022-01-25
  • 2021-08-17
  • 1970-01-01
  • 2023-03-18
  • 1970-01-01
  • 2021-07-24
  • 1970-01-01
  • 2014-06-04
  • 1970-01-01
相关资源
最近更新 更多