Avoid printing of dots

前端 未结 2 559
再見小時候
再見小時候 2020-12-04 03:28

I run pytest with option -q.

Unfortunately this prints out a lot of dots. Example:

.....................................................         


        
相关标签:
2条回答
  • 2020-12-04 03:37

    The verbosity options can't turn off the test outcome printing. However, pytest can be customized in many ways, including the outcome printing. To change this, you would override the pytest_report_teststatus hook.

    turn off short letters

    Create a file conftest.py with the following content:

    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)
    

    Now running the tests will not print any short outcome letters (.sxFE). The code is a bit verbose, but handles all the standard outcomes defined in the framework.

    turn off verbose outcomes

    When running in verbose mode, pytest prints the outcome along with the test case name:

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

    If you remove the lines setting verbose from the above hook impl (leaving it set to an empty string), pytest will also stop printing outcomes in verbose mode:

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

    introducing custom reporting mode via command line switch

    The below example will turn off printing both short and verbose outcomes when --silent flag is passed from command line:

    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)
    
    0 讨论(0)
  • 2020-12-04 03:56

    Using my plugin pytest-custom-report you can easily suppress those dots by using an empty-string marker for the report.

    pip install pytest-custom-report
    pytest -q --report-passed=""
    
    0 讨论(0)
提交回复
热议问题