pytest icon indicating copy to clipboard operation
pytest copied to clipboard

Incorrect timing info when using subtests and console_output_style='times'

Open correctmost opened this issue 7 months ago • 1 comments

pytest does not seem to report accurate timing info when using subtests and console_output_style = 'times':

pyproject.toml

[tool.pytest]
addopts = [
    '--capture=tee-sys',
    '-v'
]
console_output_style = 'times'

test_subtests.py

import time
import unittest

class Test(unittest.TestCase):
    def test_subtests(self):
        for i in range(3):
            with self.subTest(i=i):
                time.sleep(1)

Output:

$ pytest
==================================== test session starts =====================================
platform linux -- Python 3.14.4, pytest-9.1.0.dev332+gd4fde45a5, pluggy-1.6.0 -- /home/user/.venv-pyt/bin/python
cachedir: .pytest_cache
rootdir: /home/user/scratch
configfile: pyproject.toml
collected 1 item                                                                             

test_subtests.py::Test::test_subtests SUBPASSED(i=0)                                  570.0us
test_subtests.py::Test::test_subtests SUBPASSED(i=1)                                  0.000us
test_subtests.py::Test::test_subtests SUBPASSED(i=2)                                  0.000us
test_subtests.py::Test::test_subtests PASSED                                          0.000us

============================ 1 passed, 3 subtests passed in 3.02s ============================

Version info

Package   Version                 Editable project location
--------- ----------------------- -------------------------
iniconfig 2.3.0
packaging 26.1
pip       26.0.1
pluggy    1.6.0
Pygments  2.20.0
pytest    9.1.0.dev332+gd4fde45a5 /home/user/code/pytest
  • pytest 9.0.3 and d4fde45a5
  • Python 3.14.4
  • Arch Linux

correctmost avatar Apr 22 '26 22:04 correctmost

Opened a fix for this here: #14453

The issue turned out to have two parts:

  • terminal timing aggregation for subtests was keyed only by nodeid
  • unittest SubtestReport durations were always created with duration=0

The PR adds regression coverage for duplicate subTest() contexts and measures subtest duration at the public subTest() context-manager boundary during runtest().

Criseda avatar May 09 '26 15:05 Criseda