pytestpython-3.11pytest-html

How to capture pytest durations statistics within the test report


I am using the durations options in the pytest commandline to extract information of the top 5 slowest tests. Below is the information that is printed in the console at the end of tests. The test results are outputted in an html or a junit-xml report. Is there a hook to capture the durations statistics in the html or junit-xml report?

I tried the following solution but it changed nothing in the report.

Any suggestions on how to do it?

======== fixture duration top ===================================================
total            name        num   avg              min
0:00:01.219000   login_token   1   0:00:01.219000   0:00:01.219000
0:00:00.734000   login_creds   1   0:00:00.734000   0:00:00.734000
0:00:01.953000   grand total  18   0:00:00.734000          0:00:00
======== test call duration top ================================================= 
total            name                       num   avg              min
0:13:19.203000   Test_some_feature::test_01   8   0:01:38.351500   0:01:35.562000
0:04:19.500000   Test_some_feature::test_02   1   0:04:19.500000   0:04:19.500000
0:03:42.329000   Test_some_feature::test_03   8   0:00:25.195500   0:00:23.281000
0:21:21.032000                grand total    17   0:01:38.351500   0:00:23.281000
========= test setup duration top =============================================== 
total            name                        num    avg     min
       0:00:00                grand total    17     0:00:00 0:00:00
========= test teardown duration top ============================================
total            name                        num    avg      min
       0:00:00                grand total    17     0:00:00  0:00:00
========= slowest 15 durations ==================================================

thanks,

Archie


Solution

  • After much research, I was not able to find a pytest hook to port the --durations console output into the pytest html/junit-xml report. As per my earlier comment, I redirect the console output to a file, like so:

    command 2> result.txt
    

    I end up with 2 reports to parse/check to get the complete picture. I still think that there is a better solution out there. But this will have to do for now.