【问题标题】:Avoid printing of dots避免打印点
【发布时间】:2018-11-19 12:22:27
【问题描述】:

我使用选项 -q 运行 pytest。

不幸的是,这会打印出很多点。示例:

...................................................................................s...............s...................................ssssss..................................................................................................................................s..............s.........................s..............................................................................................................F....s.s............s.....................s...........................................................................................................................
=================================== FAILURES ===================================
_____________________ TestFoo.test_bar _____________________
Traceback (most recent call last):
  (cut)

有没有办法避免这个长长的点和“s”字符列表?

更新

有一个有效的答案。但不知何故,它对我来说太长了。我现在使用这个解决方法:我将它添加到调用 pytest 的脚本中:pytest -q | perl -pe 's/^[.sxFE]{20,}$//g'

【问题讨论】:

    标签: python pytest


    【解决方案1】:

    详细程度选项无法关闭测试结果打印。但是,pytest 可以通过多种方式进行自定义,包括结果打印。要更改此设置,您将覆盖 pytest_report_teststatus 挂钩。

    关闭短字母

    创建一个文件conftest.py,内容如下:

    import pytest
    
    def pytest_report_teststatus(report):
        category, short, verbose = '', '', ''
        if hasattr(report, 'wasxfail'):
            if report.skipped:
                category = 'xfailed'
                verbose = 'xfail'
            elif report.passed:
                category = 'xpassed'
                verbose = ('XPASS', {'yellow': True})
            return (category, short, verbose)
        elif report.when in ('setup', 'teardown'):
            if report.failed:
                category = 'error'
                verbose = 'ERROR'
            elif report.skipped:
                category = 'skipped'
                verbose = 'SKIPPED'
            return (category, short, verbose)
        category = report.outcome
        verbose = category.upper()
        return (category, short, verbose)
    

    现在运行测试将不会打印任何简短的结果字母 (.sxFE)。代码有点冗长,但处理了框架中定义的所有标准结果。

    关闭详细结果

    在详细模式下运行时,pytest 会打印结果以及测试用例名称:

    $ pytest -sv
    =================================== test session starts ===================================
    ...
    test_spam.py::test_spam PASSED
    test_spam.py::test_eggs FAILED
    test_spam.py::test_bacon SKIPPED
    test_spam.py::test_foo xfail
    ...
    

    如果您从上述钩子 impl 中删除行设置 verbose(将其设置为空字符串),pytest 也将停止在详细模式下打印结果:

    import pytest
    
    def pytest_report_teststatus(report):
        category, short, verbose = '', '', ''
        if hasattr(report, 'wasxfail'):
            if report.skipped:
                category = 'xfailed'
            elif report.passed:
                category = 'xpassed'
            return (category, short, verbose)
        elif report.when in ('setup', 'teardown'):
            if report.failed:
                category = 'error'
            elif report.skipped:
                category = 'skipped'
            return (category, short, verbose)
        category = report.outcome
        return (category, short, verbose)
    
    $ pytest -sv
    =================================== test session starts ===================================
    ...
    test_spam.py::test_spam
    test_spam.py::test_eggs
    test_spam.py::test_bacon
    test_spam.py::test_foo
    ...

    通过命令行开关引入自定义报告模式

    当从命令行传递--silent 标志时,以下示例将关闭打印简短和详细的结果:

    import pytest
    
    def pytest_addoption(parser):
        parser.addoption('--silent', action='store_true', default=False)
    
    
    def pytest_report_teststatus(report, config):
        category, short, verbose = '', '', ''
        if not config.getoption('--silent'):
            return None
    
        if hasattr(report, 'wasxfail'):
            if report.skipped:
                category = 'xfailed'
            elif report.passed:
                category = 'xpassed'
            return (category, short, verbose)
        elif report.when in ('setup', 'teardown'):
            if report.failed:
                category = 'error'
            elif report.skipped:
                category = 'skipped'
            return (category, short, verbose)
        category = report.outcome
        return (category, short, verbose)
    

    【讨论】:

    • 显然,Pytest 中的全局配置对象已被弃用,至少在 6.2 中,上面的代码不再运行 (AttributeError: module 'pytest' has no attribute 'config')。如果有人可以对这段代码进行现代化改造,那就太好了。
    • @JeffWright pytest_report_teststatus 现在也接受 config 对象作为参数,因此更新很容易 - 在答案中查看更新的 sn-p。
    【解决方案2】:

    使用我的插件pytest-custom-report,您可以通过在报告中使用空字符串标记轻松抑制这些点。

    pip install pytest-custom-report
    pytest -q --report-passed=""
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-11-27
      • 1970-01-01
      • 1970-01-01
      • 2013-12-20
      • 2020-11-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多