【问题标题】:How to print only the summary with pytest?如何使用 pytest 仅打印摘要?
【发布时间】:2021-02-15 04:21:03
【问题描述】:

我希望 pytest 在运行我的测试时只打印摘要。我正在使用 -s 标志,以便传递其他输出。即使使用 --quiet 它也会打印小绿点(如果成功)。我想压制那些。明确控制测试后打印的内容也很好,这样我就可以用空格或其他东西分隔输出,但这是一个不同的问题。

我这样做是为了检查 CLI 的输出。

【问题讨论】:

  • pytest-custom-report 插件对你有帮助吗?
  • 感谢您的建议,但总体而言似乎很不理想。
  • @hoefling 感谢您的指导。看看我的涂料解决方案。顺便说一句,近十年来我一直很欣赏你的头发。
  • @MatrixTheatrics 我不明白,pytest -q --report-passed="" -s 还没有在这里做你想做的事情吗?在您的“涂料解决方案”中,它只是使用插件已经使用的相同钩子,但它是以丑陋和非通用的方式完成的。

标签: python unit-testing pytest


【解决方案1】:

使用我的插件pytest-custom-report,您可以自定义用于报告字符的所有字形。使用空字符串可以有效地隐藏它们。

pytest -q --report-passed="" -s

将-q 传递给 pytest 还会隐藏测试文件名。

【讨论】:

    【解决方案2】:

    hoefling 让我朝着正确的方向前进。通过设置short str,您可以自定义输出什么而不是点;转发元组的其余元素,同时将其保留为空字符串就足够了。我什至做了一些……你自己看看。不推荐使用 --verbose。可以删除更多的部分,那些处理失败案例的部分,但现在不关心了。

    A nice picture of the output.(还没有代表在这里发布)

    from _pytest.reports import CollectReport, TestReport, Config
    from typing import Tuple, Union, Mapping
    import py
    
    def pytest_report_teststatus(report: Union[CollectReport, TestReport], config: Config, files_set=set()) \
            -> Tuple[str, str, Union[str, Mapping[str, bool]]]:
        def get_line_delim():
            n = py.io.TerminalWriter().fullwidth // 3
            return f'\n{"*  "*n}\n'
        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'
            elif report.when == 'setup' and report.location[0] not in files_set:
                files_set.add(report.location[0]) # needed so that the first test for each file can start out clean
                return (category, get_line_delim(), (verbose, {'red': True}))
            return (category, short, verbose)
        category = report.outcome
        verbose = category.upper()
        return (category, get_line_delim(), (verbose, {'red': True}))
    

    【讨论】:

      猜你喜欢
      • 2023-01-05
      • 2022-11-20
      • 2013-06-20
      • 1970-01-01
      • 1970-01-01
      • 2016-11-21
      • 2012-06-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多