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

2024/10/13 21:19:15

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.

Answer

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

# conftest.pydef 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._sessionstarttimeprint('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.

https://en.xdnf.cn/q/69487.html

Related Q&A

Tensorflow ArgumentError Running CIFAR-10 example

I am trying to run the CIFAR-10 example of Tensorflow. However when executing python cifar10.py I am getting the error attached below. I have installed Version 0.6.0 of the Tensorflow package using pip…

How to plot multiple density plots on the same figure in python

I know this is going to end up being a really messy plot, but I am curious to know what the most efficient way to do this is. I have some data that looks like this in a csv file:ROI Band Min…

Running Tkinter on Mac

I am an absolute newbie. Im trying to make Python GUI for my school project so I decided to use Tkinter. When I try to import Tkinter it throws this message: >>> import tkinter Traceback (most…

Object-based default value in SQLAlchemy declarative

With SQLAlchemy, it is possible to add a default value to every function. As I understand it, this may also be a callable (either without any arguments or with an optional ExecutionContext argument).No…

High Resolution Image of a Graph using NetworkX and Matplotlib

I have a python code to generate a random graph of 300 nodes and 200 edges and display itimport networkx as nx import matplotlib.pyplot as pltG = nx.gnm_random_graph(300,200) graph_pos = nx.spring_layo…

how to read an outputted fortran binary NxNxN matrix into Python

I wrote out a matrix in Fortran as follows:real(kind=kind(0.0d0)), dimension(256,256,256) :: dense[...CALCULATION...]inquire(iolength=reclen)dense open(unit=8,file=fname,& form=unformatted,access=d…

python argparse subcommand with dependency and conflict

I want to use argparse to build a tool with subcommand. The possible syntax could be/tool.py download --from 1234 --interval 60 /tool.py download --build 1432 /tool.py clean --numbers 10So I want to us…

Running django project without django installation

I have developed a project with Django framework(python and mysql DB) in Linux OS(Ubuntu 12.04), I want to run this project in localhost in another machine with Linux(Ubuntu 12.04) without installing D…

redefine __and__ operator

Why I cant redefine the __and__ operator?class Cut(object):def __init__(self, cut):self.cut = cutdef __and__(self, other):return Cut("(" + self.cut + ") && (" + other.cut +…

How to find class of bound method during class construction in Python 3.1?

i want to write a decorator that enables methods of classes to become visible to other parties; the problem i am describing is, however, independent of that detail. the code will look roughly like this…