Skip to content

Commit 877d53e

Browse files
committed
tests: 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 13ce91d commit 877d53e

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
@@ -66,13 +66,13 @@
6666
)
6767
from testtools.testresult.doubles import (
6868
ExtendedTestResult,
69-
Python3TestResult,
7069
TwistedTestResult,
7170
_StatusEvent,
7271
)
7372
from testtools.testresult.doubles import (
7473
StreamResult as LoggingStreamResult,
7574
)
75+
from testtools.testresult.doubles import TestResult as LoggingTestResult
7676
from testtools.testresult.real import (
7777
_details_to_str,
7878
_merge_tags,
@@ -467,14 +467,14 @@ def makeResult(self):
467467
return ExtendedTestResult()
468468

469469

470-
class TestPython3TestResultContract(TestCase, Python3Contract):
470+
class TestLoggingTestResultContract(TestCase, Python3Contract):
471471
def makeResult(self):
472-
return Python3TestResult()
472+
return LoggingTestResult()
473473

474474

475-
class TestAdaptedPython3TestResultContract(TestCase, DetailsContract):
475+
class TestAdaptedLoggingTestResultContract(TestCase, DetailsContract):
476476
def makeResult(self):
477-
return ExtendedToOriginalDecorator(Python3TestResult())
477+
return ExtendedToOriginalDecorator(LoggingTestResult())
478478

479479

480480
class TestAdaptedTwistedTestResultContract(TestCase, DetailsContract):
@@ -494,21 +494,21 @@ def makeResult(self):
494494
return TestResultDecorator(TestResult())
495495

496496

497-
class TestPython3TestResultDuration(TestCase):
498-
"""Tests for addDuration functionality in Python3TestResult."""
497+
class TestLoggingTestResultDuration(TestCase):
498+
"""Tests for addDuration functionality in LoggingTestResult."""
499499

500500
def test_addDuration_logging(self):
501-
# Python3TestResult should log addDuration calls
502-
result = Python3TestResult()
501+
# LoggingTestResult should log addDuration calls
502+
result = LoggingTestResult()
503503
test = make_erroring_test()
504504

505505
result.addDuration(test, 1.5)
506506
expected_call = ("addDuration", test, 1.5)
507507
self.assertIn(expected_call, result._events)
508508

509509
def test_addDuration_stores_in_collectedDurations(self):
510-
# Python3TestResult should store durations in collectedDurations
511-
result = Python3TestResult()
510+
# LoggingTestResult should store durations in collectedDurations
511+
result = LoggingTestResult()
512512
test = make_erroring_test()
513513

514514
result.addDuration(test, 2.3)
@@ -2888,7 +2888,7 @@ def testStopTestRun(self):
28882888

28892889
class TestExtendedToOriginalResultDecoratorBase(TestCase):
28902890
def make_result(self):
2891-
self.result = Python3TestResult()
2891+
self.result = LoggingTestResult()
28922892
self.make_converter()
28932893

28942894
def make_converter(self):

tests/test_testsuite.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
iterate_tests,
1818
)
1919
from testtools.matchers import DocTestMatches, Equals
20-
from testtools.testresult.doubles import StreamResult as LoggingStream
20+
from testtools.testresult.doubles import StreamResult
2121
from testtools.testsuite import FixtureSuite, sorted_tests
2222

2323
from .helpers import LoggingResult
@@ -103,7 +103,7 @@ def split_suite(self, suite):
103103

104104
class TestConcurrentStreamTestSuiteRun(TestCase):
105105
def test_trivial(self):
106-
result = LoggingStream()
106+
result = StreamResult()
107107
test1 = Sample("test_method1")
108108
test2 = Sample("test_method2")
109109

@@ -193,7 +193,7 @@ def __call__(self):
193193
def run(self):
194194
pass
195195

196-
result = LoggingStream()
196+
result = StreamResult()
197197

198198
def cases():
199199
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)