Skip to content

Commit 7db4479

Browse files
committed
Rework, rename Python3TestResult
As the module name suggests, this is a test "double" for the stdlib unittest.TestResult that logs calls. Make it inherit from the test case so we can see this at a typing level. Signed-off-by: Stephen Finucane <stephen@that.guru>
1 parent 121c3a1 commit 7db4479

File tree

6 files changed

+76
-66
lines changed

6 files changed

+76
-66
lines changed

doc/for-framework-folk.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,7 @@ Test Doubles
362362
------------
363363

364364
In testtools.testresult.doubles there are several test doubles that testtools
365-
uses for its own testing: ``Python3TestResult``, and ``ExtendedTestResult``.
365+
uses for its own testing: ``LoggingTestResult``, and ``ExtendedTestResult``.
366366
These TestResult objects implement a single variation of
367367
the TestResult API each, and log activity to a list ``self._events``. These are
368368
made available for the convenience of people writing their own extensions.

tests/test_testcase.py

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@
4848
)
4949
from testtools.testresult.doubles import (
5050
ExtendedTestResult,
51-
Python3TestResult,
51+
LogEvent,
52+
TestResult,
5253
)
5354

5455
from .helpers import (
@@ -1270,7 +1271,7 @@ def test(self):
12701271

12711272
def test_raising__UnexpectedSuccess_py3(self):
12721273
case = self.make_unexpected_case()
1273-
result = Python3TestResult()
1274+
result = TestResult()
12741275
case.run(result)
12751276
case = result._events[0][1]
12761277
self.assertEqual(
@@ -1775,8 +1776,8 @@ class SkippingTest(TestCase):
17751776
def test_that_raises_skipException(self):
17761777
self.skipTest("skipping this test")
17771778

1778-
events: list[tuple[str, ...]] = []
1779-
result = Python3TestResult(events)
1779+
events: list[LogEvent] = []
1780+
result = TestResult(events)
17801781
test = SkippingTest("test_that_raises_skipException")
17811782
test.run(result)
17821783
case = result._events[0][1]
@@ -1796,8 +1797,8 @@ class SkippingTest(TestCase):
17961797
def test_that_raises_skipException(self):
17971798
self.skipTest("skipping this test")
17981799

1799-
events: list[tuple[str, ...]] = []
1800-
result = Python3TestResult(events)
1800+
events: list[LogEvent] = []
1801+
result = TestResult(events)
18011802
test = SkippingTest("test_that_raises_skipException")
18021803
test.run(result)
18031804
case = result._events[0][1]
@@ -1819,8 +1820,8 @@ def setUp(self):
18191820
def test_that_raises_skipException(self):
18201821
pass
18211822

1822-
events: list[tuple[str, ...]] = []
1823-
result = Python3TestResult(events)
1823+
events: list[LogEvent] = []
1824+
result = TestResult(events)
18241825
test = SkippingTest("test_that_raises_skipException")
18251826
test.run(result)
18261827
self.assertEqual("addSkip", events[1][0])
@@ -1830,8 +1831,8 @@ class SkippingTest(TestCase):
18301831
def test_that_raises_skipException(self):
18311832
raise self.skipException("skipping this test")
18321833

1833-
events: list[tuple[str, ...]] = []
1834-
result = Python3TestResult(events)
1834+
events: list[LogEvent] = []
1835+
result = TestResult(events)
18351836
test = SkippingTest("test_that_raises_skipException")
18361837
test.run(result)
18371838
self.assertEqual("addSkip", events[1][0])
@@ -1842,8 +1843,8 @@ class SkippingTest(TestCase):
18421843
def test_that_is_decorated_with_skip(self):
18431844
self.fail()
18441845

1845-
events: list[tuple[str, ...]] = []
1846-
result = Python3TestResult(events)
1846+
events: list[LogEvent] = []
1847+
result = TestResult(events)
18471848
test = SkippingTest("test_that_is_decorated_with_skip")
18481849
test.run(result)
18491850
self.assertEqual("addSkip", events[1][0])
@@ -1854,8 +1855,8 @@ class SkippingTest(TestCase):
18541855
def test_that_is_decorated_with_skipIf(self):
18551856
self.fail()
18561857

1857-
events: list[tuple[str, ...]] = []
1858-
result = Python3TestResult(events)
1858+
events: list[LogEvent] = []
1859+
result = TestResult(events)
18591860
test = SkippingTest("test_that_is_decorated_with_skipIf")
18601861
test.run(result)
18611862
self.assertEqual("addSkip", events[1][0])
@@ -1866,8 +1867,8 @@ class SkippingTest(TestCase):
18661867
def test_that_is_decorated_with_skipUnless(self):
18671868
self.fail()
18681869

1869-
events: list[tuple[str, ...]] = []
1870-
result = Python3TestResult(events)
1870+
events: list[LogEvent] = []
1871+
result = TestResult(events)
18711872
test = SkippingTest("test_that_is_decorated_with_skipUnless")
18721873
test.run(result)
18731874
self.assertEqual("addSkip", events[1][0])
@@ -1882,14 +1883,14 @@ class SkippingTest(TestCase):
18821883
class NotSkippingTest(TestCase):
18831884
test_no_skip = skipIf(False, "skipping this test")(shared)
18841885

1885-
events: list[tuple[str, ...]] = []
1886-
result = Python3TestResult(events)
1886+
events: list[LogEvent] = []
1887+
result = TestResult(events)
18871888
test = SkippingTest("test_skip")
18881889
test.run(result)
18891890
self.assertEqual("addSkip", events[1][0])
18901891

1891-
events2: list[tuple[str, ...]] = []
1892-
result2 = Python3TestResult(events2)
1892+
events2: list[LogEvent] = []
1893+
result2 = TestResult(events2)
18931894
test2 = NotSkippingTest("test_no_skip")
18941895
test2.run(result2)
18951896
self.assertEqual("addFailure", events2[1][0])
@@ -1900,8 +1901,8 @@ class SkippingTest(TestCase):
19001901
def test_that_is_decorated_with_skip(self):
19011902
self.fail()
19021903

1903-
events: list[tuple[str, ...]] = []
1904-
result = Python3TestResult(events)
1904+
events: list[LogEvent] = []
1905+
result = TestResult(events)
19051906
try:
19061907
test = SkippingTest("test_that_is_decorated_with_skip")
19071908
except unittest.SkipTest:
@@ -1915,8 +1916,8 @@ class SkippingTest(TestCase):
19151916
def test_that_is_decorated_with_skipIf(self):
19161917
self.fail()
19171918

1918-
events: list[tuple[str, ...]] = []
1919-
result = Python3TestResult(events)
1919+
events: list[LogEvent] = []
1920+
result = TestResult(events)
19201921
try:
19211922
test = SkippingTest("test_that_is_decorated_with_skipIf")
19221923
except unittest.SkipTest:
@@ -1930,8 +1931,8 @@ class SkippingTest(TestCase):
19301931
def test_that_is_decorated_with_skipUnless(self):
19311932
self.fail()
19321933

1933-
events: list[tuple[str, ...]] = []
1934-
result = Python3TestResult(events)
1934+
events: list[LogEvent] = []
1935+
result = TestResult(events)
19351936
try:
19361937
test = SkippingTest("test_that_is_decorated_with_skipUnless")
19371938
except unittest.SkipTest:
@@ -2024,7 +2025,7 @@ def method(self):
20242025
self.assertThat(events, Equals([True]))
20252026

20262027
def test_added_handler_works(self):
2027-
events: list[tuple[str, ...]] = []
2028+
events: list[LogEvent] = []
20282029

20292030
class Case(TestCase):
20302031
def method(self):
@@ -2036,7 +2037,7 @@ def method(self):
20362037
self.assertThat(events, Equals([an_exc_info]))
20372038

20382039
def test_handler_that_raises_is_not_caught(self):
2039-
events: list[tuple[str, ...]] = []
2040+
events: list[LogEvent] = []
20402041

20412042
class Case(TestCase):
20422043
def method(self):

tests/test_testresult.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,13 @@
6464
)
6565
from testtools.testresult.doubles import (
6666
ExtendedTestResult,
67-
Python3TestResult,
6867
TwistedTestResult,
6968
_StatusEvent,
7069
)
7170
from testtools.testresult.doubles import (
7271
StreamResult as LoggingStreamResult,
7372
)
73+
from testtools.testresult.doubles import TestResult as LoggingTestResult
7474
from testtools.testresult.real import (
7575
_details_to_str,
7676
_merge_tags,
@@ -470,14 +470,14 @@ def makeResult(self):
470470
return ExtendedTestResult()
471471

472472

473-
class TestPython3TestResultContract(TestCase, Python3Contract):
473+
class TestLoggingTestResultContract(TestCase, Python3Contract):
474474
def makeResult(self):
475-
return Python3TestResult()
475+
return LoggingTestResult()
476476

477477

478-
class TestAdaptedPython3TestResultContract(TestCase, DetailsContract):
478+
class TestAdaptedLoggingTestResultContract(TestCase, DetailsContract):
479479
def makeResult(self):
480-
return ExtendedToOriginalDecorator(Python3TestResult())
480+
return ExtendedToOriginalDecorator(LoggingTestResult())
481481

482482

483483
class TestAdaptedTwistedTestResultContract(TestCase, DetailsContract):
@@ -497,21 +497,21 @@ def makeResult(self):
497497
return TestResultDecorator(TestResult())
498498

499499

500-
class TestPython3TestResultDuration(TestCase):
501-
"""Tests for addDuration functionality in Python3TestResult."""
500+
class TestLoggingTestResultDuration(TestCase):
501+
"""Tests for addDuration functionality in LoggingTestResult."""
502502

503503
def test_addDuration_logging(self):
504-
# Python3TestResult should log addDuration calls
505-
result = Python3TestResult()
504+
# LoggingTestResult should log addDuration calls
505+
result = LoggingTestResult()
506506
test = make_erroring_test()
507507

508508
result.addDuration(test, 1.5)
509509
expected_call = ("addDuration", test, 1.5)
510510
self.assertIn(expected_call, result._events)
511511

512512
def test_addDuration_stores_in_collectedDurations(self):
513-
# Python3TestResult should store durations in collectedDurations
514-
result = Python3TestResult()
513+
# LoggingTestResult should store durations in collectedDurations
514+
result = LoggingTestResult()
515515
test = make_erroring_test()
516516

517517
result.addDuration(test, 2.3)
@@ -2896,7 +2896,7 @@ def testStopTestRun(self):
28962896

28972897
class TestExtendedToOriginalResultDecoratorBase(TestCase):
28982898
def make_result(self):
2899-
self.result = Python3TestResult()
2899+
self.result = LoggingTestResult()
29002900
self.make_converter()
29012901

29022902
def make_converter(self):

tests/test_testsuite.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
iterate_tests,
1616
)
1717
from testtools.matchers import DocTestMatches, Equals
18-
from testtools.testresult.doubles import StreamResult as LoggingStream
18+
from testtools.testresult.doubles import StreamResult
1919
from testtools.testsuite import FixtureSuite, sorted_tests
2020

2121
from .helpers import LoggingResult
@@ -106,7 +106,7 @@ def split_suite(self, suite):
106106

107107
class TestConcurrentStreamTestSuiteRun(TestCase):
108108
def test_trivial(self):
109-
result = LoggingStream()
109+
result = StreamResult()
110110
test1 = Sample("test_method1")
111111
test2 = Sample("test_method2")
112112

@@ -196,7 +196,7 @@ def __call__(self):
196196
def run(self):
197197
pass
198198

199-
result = LoggingStream()
199+
result = StreamResult()
200200

201201
def cases():
202202
return [(BrokenTest(), "0")]

testtools/runtest.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ def _run_one(self, result: "TestResult") -> "TestResult":
112112
"""
113113
return self._run_prepared_result(ExtendedToOriginalDecorator(result)) # type: ignore[arg-type]
114114

115-
def _run_prepared_result(self, result: "TestResult") -> "TestResult":
115+
def _run_prepared_result(self, result: TestResult) -> TestResult:
116116
"""Run one test reporting to result.
117117
118118
:param result: A testtools.TestResult to report activity to.
@@ -189,7 +189,7 @@ def _run_core(self) -> None:
189189
self.case, details=self.case.getDetails()
190190
)
191191

192-
def _run_cleanups(self, result: "TestResult") -> object | None:
192+
def _run_cleanups(self, result: TestResult) -> object | None:
193193
"""Run the cleanups that have been added with addCleanup.
194194
195195
See the docstring for addCleanup for more information.

0 commit comments

Comments
 (0)