python - 是否可以将 xdist 网关编号打印到 stdout 中的每一行?

标签 python pytest xdist

我正在使用日志记录模块打印到标准输出。 当我使用 pytest-xdist 运行测试时,很难理解什么消息对应于worker

是否可以将 xdist 网关编号打印到 stdout 中的每一行?

我现在拥有的日志消息示例:

[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

期望:

[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

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

最佳答案

您可以通过config.slaveinput属性访问网关ID。示例:

def test_spam(pytestconfig):
    assert hasattr(pytestconfig, 'slaveinput')
    assert pytestconfig.slaveinput['slaveid'] == 'gw0'

仅当实际调用 xdist 时测试才会通过,例如通过pytest -n1 test_spam.py,否则slaveinput属性将不会被设置。

关于python - 是否可以将 xdist 网关编号打印到 stdout 中的每一行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48934551/

相关文章:

python - get_dummies 在 pandas 中的用法

python - 提高在列表中查找项目索引的速度

python - 使用 Pycharm pytest runner 时如何将插件选项传递给 pytest?

python - 如何将额外的参数传递给我的 py.test 设置方法?

python - pytest 固定装置和线程同步

python - Scrapy中的爬行顺序

python - 与 tif 一起保存图像元数据的最佳方法是什么?

python - 在pytest中人为地提前时间

pytest-xdist 导致 AttributeError : 'dict' object has no attribute 'style'

python - 为什么 pytest + xdist 不捕获输出?