Skip to content

Commit 32e1d83

Browse files
author
Michael Foord
committed
Enable unittest.TestCase to be instantiated without providing a method name.
Changed unittestgui to show number of discovered tests in the status bar.
1 parent faa8c13 commit 32e1d83

File tree

5 files changed

+30
-5
lines changed

5 files changed

+30
-5
lines changed

Doc/library/unittest.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -721,6 +721,11 @@ Test cases
721721
Here, we create two instances of :class:`WidgetTestCase`, each of which runs a
722722
single test.
723723

724+
.. versionchanged::
725+
`TestCase` can be instantiated successfully without providing a method
726+
name. This makes it easier to experiment with `TestCase` from the
727+
interactive interpreter.
728+
724729
*methodName* defaults to :meth:`runTest`.
725730

726731
:class:`TestCase` instances provide three groups of methods: one group used

Lib/unittest/case.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -274,12 +274,17 @@ def __init__(self, methodName='runTest'):
274274
"""
275275
self._testMethodName = methodName
276276
self._outcomeForDoCleanups = None
277+
self._testMethodDoc = 'No test'
277278
try:
278279
testMethod = getattr(self, methodName)
279280
except AttributeError:
280-
raise ValueError("no such test method in %s: %s" %
281-
(self.__class__, methodName))
282-
self._testMethodDoc = testMethod.__doc__
281+
if methodName != 'runTest':
282+
# we allow instantiation with no explicit method name
283+
# but not an *incorrect* or missing method name
284+
raise ValueError("no such test method in %s: %s" %
285+
(self.__class__, methodName))
286+
else:
287+
self._testMethodDoc = testMethod.__doc__
283288
self._cleanups = []
284289

285290
# Map types to custom assertEqual functions that will compare

Lib/unittest/test/test_case.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,16 @@ def test(self): pass
7777

7878
self.assertEqual(Test().id()[-13:], '.Test.runTest')
7979

80+
# test that TestCase can be instantiated with no args
81+
# primarily for use at the interactive interpreter
82+
test = unittest.TestCase()
83+
test.assertEqual(3, 3)
84+
with test.assertRaises(test.failureException):
85+
test.assertEqual(3, 2)
86+
87+
with self.assertRaises(AttributeError):
88+
test.run()
89+
8090
# "class TestCase([methodName])"
8191
# ...
8292
# "Each instance of TestCase will run a single test method: the

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ Core and Builtins
2323
Library
2424
-------
2525

26+
- `unittest.TestCase` can be instantiated without a method name; for simpler
27+
exploration from the interactive interpreter.
28+
2629
- Issue #10798: Reject supporting concurrent.futures if the system has too
2730
few POSIX semaphores.
2831

Tools/unittestgui/unittestgui.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -276,13 +276,15 @@ def settingsClicked(self):
276276
self.test_file_glob_pattern = d.test_file_glob_pattern
277277

278278
def notifyTestsDiscovered(self, test_suite):
279+
discovered = test_suite.countTestCases()
279280
self.runCountVar.set(0)
280281
self.failCountVar.set(0)
281282
self.errorCountVar.set(0)
282-
self.remainingCountVar.set(test_suite.countTestCases())
283+
self.remainingCountVar.set(discovered)
283284
self.progressBar.setProgressFraction(0.0)
284285
self.errorListbox.delete(0, tk.END)
285-
self.statusVar.set("Discovering tests from %s" % self.directory_to_read)
286+
self.statusVar.set("Discovering tests from %s. Found: %s" %
287+
(self.directory_to_read, discovered))
286288
self.stopGoButton['state'] = tk.NORMAL
287289

288290
def createWidgets(self):

0 commit comments

Comments
 (0)