Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the total number of tests passed, failed and skipped from pytest

How can I get to the statistics information of a test session in pytest?

I've tried to define pytest_sessionfinish in the conftest.py file, but I only see testsfailed and testscollected attributes on there.

I also need to know the number of tests passed, skipped and the total time it took. Since pytest prints that info at the end of each session, I assume there's a programmatic way to retrieve that.

like image 305
MvdD Avatar asked Feb 11 '19 17:02

MvdD


People also ask

How do I skip tests in pytest?

The simplest way to skip a test is to mark it with the skip decorator which may be passed an optional reason . It is also possible to skip imperatively during test execution or setup by calling the pytest. skip(reason) function. This is useful when it is not possible to evaluate the skip condition during import time.

How do you run failed test cases in pytest?

The plugin provides two command line options to rerun failures from the last pytest invocation: --lf , --last-failed - to only re-run the failures. --ff , --failed-first - to run the failures first and then the rest of the tests.

What is pytest Mark skip?

If you want to skip all test functions of a module, you may use the pytestmark global: # test_module.py pytestmark = pytest. mark. skipif(...) If multiple skipif decorators are applied to a test function, it will be skipped if any of the skip conditions is true.

What is pytest Hookimpl?

If the hook method is labeled as hookwrapper=True , pytest will execute the part before yield first and then execute other same type hook methods. After these methods executed, the part after yield will be executed. (This feature is just like pytest fixtures.)


1 Answers

Use the pytest_terminal_summary hook. The stats are provided by the terminalreporter object. Example:

# conftest.py

def pytest_terminal_summary(terminalreporter, exitstatus, config):
    print('passed amount:', len(terminalreporter.stats['passed']))
    print('failed amount:', len(terminalreporter.stats['failed']))
    print('xfailed amount:', len(terminalreporter.stats['xfailed']))
    print('skipped amount:', len(terminalreporter.stats['skipped']))

    duration = time.time() - terminalreporter._sessionstarttime
    print('duration:', duration, 'seconds')

Unfortunately, _pytest.terminal.TerminalReporter isn't part of the public API yet, so it's best to inspect its code directly.


If you need to access the stats in another hook like pytest_sessionfinish, use the plugin manager, e.g.:

def pytest_sessionfinish(session, exitstatus):
    reporter = session.config.pluginmanager.get_plugin('terminalreporter')
    print('passed amount:', len(reporter.stats['passed']))
    ...

However, depending on what hook you are in, you may not get the complete stats/correct duration, so caution advised.

like image 86
hoefling Avatar answered Nov 15 '22 04:11

hoefling