@@ -478,6 +478,75 @@ def makeResult(self):
478478 return TestResultDecorator (TestResult ())
479479
480480
481+ class TestPython3TestResultDuration (TestCase ):
482+ """Tests for addDuration functionality in Python3TestResult."""
483+
484+ def test_addDuration_logging (self ):
485+ # Python3TestResult should log addDuration calls
486+ result = Python3TestResult ()
487+ test = make_erroring_test ()
488+
489+ result .addDuration (test , 1.5 )
490+ expected_call = ("addDuration" , test , 1.5 )
491+ self .assertIn (expected_call , result ._events )
492+
493+ def test_addDuration_stores_in_collectedDurations (self ):
494+ # Python3TestResult should store durations in collectedDurations
495+ result = Python3TestResult ()
496+ test = make_erroring_test ()
497+
498+ result .addDuration (test , 2.3 )
499+ self .assertEqual ([(test , 2.3 )], result .collectedDurations )
500+
501+ # Multiple durations should accumulate
502+ result .addDuration (test , 1.7 )
503+ self .assertEqual ([(test , 2.3 ), (test , 1.7 )], result .collectedDurations )
504+
505+
506+ class TestExtendedTestResultDuration (TestCase ):
507+ """Tests for addDuration functionality in ExtendedTestResult."""
508+
509+ def test_addDuration_logging (self ):
510+ # ExtendedTestResult should log addDuration calls
511+ result = ExtendedTestResult ()
512+ test = make_erroring_test ()
513+
514+ result .addDuration (test , 3.1 )
515+ expected_call = ("addDuration" , test , 3.1 )
516+ self .assertIn (expected_call , result ._events )
517+
518+
519+ class TestTestResultDecoratorDuration (TestCase ):
520+ """Tests for addDuration functionality in TestResultDecorator."""
521+
522+ def test_addDuration_forwards_to_decorated_result (self ):
523+ # TestResultDecorator should forward addDuration calls to decorated result
524+ base_result = TestResult ()
525+ base_result .startTestRun () # Initialize collectedDurations
526+ decorator = TestResultDecorator (base_result )
527+ test = make_erroring_test ()
528+
529+ decorator .addDuration (test , 4.2 )
530+
531+ # Check that the duration was stored in the base result
532+ self .assertEqual ([(test , 4.2 )], base_result .collectedDurations )
533+
534+ def test_addDuration_multiple_forwards (self ):
535+ # Multiple addDuration calls should all be forwarded
536+ base_result = TestResult ()
537+ base_result .startTestRun ()
538+ decorator = TestResultDecorator (base_result )
539+ test1 = make_erroring_test ()
540+ test2 = make_erroring_test ()
541+
542+ decorator .addDuration (test1 , 1.1 )
543+ decorator .addDuration (test2 , 2.2 )
544+ decorator .addDuration (test1 , 3.3 )
545+
546+ expected = [(test1 , 1.1 ), (test2 , 2.2 ), (test1 , 3.3 )]
547+ self .assertEqual (expected , base_result .collectedDurations )
548+
549+
481550# DetailsContract because ExtendedToStreamDecorator follows Python for
482551# uxsuccess handling.
483552class TestStreamToExtendedContract (TestCase , DetailsContract ):
@@ -1521,6 +1590,61 @@ def test_traceback_with_locals(self):
15211590 ),
15221591 )
15231592
1593+ def test_addDuration (self ):
1594+ # addDuration stores test-duration pairs in collectedDurations
1595+ result = self .makeResult ()
1596+ test1 = make_erroring_test ()
1597+ test2 = make_erroring_test ()
1598+
1599+ # Initially collectedDurations should be empty after startTestRun
1600+ result .startTestRun ()
1601+ self .assertEqual ([], result .collectedDurations )
1602+
1603+ # Adding durations should store them as (test, duration) tuples
1604+ result .addDuration (test1 , 1.5 )
1605+ self .assertEqual ([(test1 , 1.5 )], result .collectedDurations )
1606+
1607+ result .addDuration (test2 , 2.3 )
1608+ self .assertEqual ([(test1 , 1.5 ), (test2 , 2.3 )], result .collectedDurations )
1609+
1610+ # Adding duration for same test should create separate entries
1611+ result .addDuration (test1 , 3.7 )
1612+ expected = [(test1 , 1.5 ), (test2 , 2.3 ), (test1 , 3.7 )]
1613+ self .assertEqual (expected , result .collectedDurations )
1614+
1615+ def test_addDuration_with_zero_duration (self ):
1616+ # addDuration should work with zero duration
1617+ result = self .makeResult ()
1618+ test = make_erroring_test ()
1619+ result .startTestRun ()
1620+
1621+ result .addDuration (test , 0.0 )
1622+ self .assertEqual ([(test , 0.0 )], result .collectedDurations )
1623+
1624+ def test_addDuration_with_negative_duration (self ):
1625+ # addDuration should work with negative duration (edge case)
1626+ result = self .makeResult ()
1627+ test = make_erroring_test ()
1628+ result .startTestRun ()
1629+
1630+ result .addDuration (test , - 1.0 )
1631+ self .assertEqual ([(test , - 1.0 )], result .collectedDurations )
1632+
1633+ def test_collectedDurations_resets_on_startTestRun (self ):
1634+ # collectedDurations should be reset when startTestRun is called
1635+ result = self .makeResult ()
1636+ test = make_erroring_test ()
1637+
1638+ # Add some durations
1639+ result .startTestRun ()
1640+ result .addDuration (test , 1.0 )
1641+ result .addDuration (test , 2.0 )
1642+ self .assertEqual ([(test , 1.0 ), (test , 2.0 )], result .collectedDurations )
1643+
1644+ # Starting a new test run should reset
1645+ result .startTestRun ()
1646+ self .assertEqual ([], result .collectedDurations )
1647+
15241648
15251649class TestMultiTestResult (TestCase ):
15261650 """Tests for 'MultiTestResult'."""
0 commit comments