避免打印点

发布于 2021-01-29 18:19:09

我用option运行pytest -q

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

...................................................................................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'

关注者
0
被浏览
36
1 个回答
  • 面试哥
    面试哥 2021-01-29
    为面试而生,有面试问题,就找面试哥。

    详细选项无法关闭测试结果打印。但是,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
    ...
    

    如果您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):
        category, short, verbose = '', '', ''
        if not pytest.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)
    


知识点
面圈网VIP题库

面圈网VIP题库全新上线,海量真题题库资源。 90大类考试,超10万份考试真题开放下载啦

去下载看看