相关疑难解决方法(0)

在py.test测试中记录

我想在测试函数中放入一些日志语句来检查一些状态变量.

我有以下代码片段:

import pytest,os
import logging

logging.basicConfig(level=logging.DEBUG)
mylogger = logging.getLogger()

#############################################################################

def setup_module(module):
    ''' Setup for the entire module '''
    mylogger.info('Inside Setup')
    # Do the actual setup stuff here
    pass

def setup_function(func):
    ''' Setup for test functions '''
    if func == test_one:
        mylogger.info(' Hurray !!')

def test_one():
    ''' Test One '''
    mylogger.info('Inside Test 1')
    #assert 0 == 1
    pass

def test_two():
    ''' Test Two '''
    mylogger.info('Inside Test 2')
    pass

if __name__ == '__main__':
    mylogger.info(' About to start the tests ') …
Run Code Online (Sandbox Code Playgroud)

python logging pytest

66
推荐指数
5
解决办法
4万
查看次数

将 pytest 日志保存在文件中

我见过这个问题:py.test logging messages and test results/assertions into a single file 我还在这里阅读了文档:https : //docs.pytest.org/en/latest/logging.html

两者都接近于令人满意的解决方案。

  1. 我不需要断言结果和日志,但如果它们都在日志中就可以了。
  2. 我需要测试期间生成的所有日志,但我不需要它们用于测试本身。在测试失败/成功后,我需要它们来分析测试结果。
  3. 我需要成功和失败测试的日志。
  4. 我需要标准输出只包含摘要(例如test-name PASSED)。如果摘要还包含失败测试的堆栈跟踪,那也没关系,但这不是必需的。
  5. 本质上,我需要测试产生 3 个不同的输出:
    1. 用于 CI 的 HTML/JUnit XML 工件。
    2. CI 日志的 CLI 最小输出。
    3. 供测试人员/自动化团队分析的扩展日志。

我试过pytest-logs插件。据我所知,它可以pytest通过在测试运行时显示所有日志记录来覆盖某些默认行为。这比默认行为略好,但离我需要的还很远。从文档中我了解到这pytest-catchlog将与 冲突pytest,我什至不想探索该选项。

这是否可以通过配置来实现,pytest或者我应该编写一个插件,或者,甚至一个插件都不会这样做,我将不得不修补pytest

python logging pytest

5
推荐指数
1
解决办法
1810
查看次数

标签 统计

logging ×2

pytest ×2

python ×2