Skip to content

Commit

Permalink
Enable unittest.TestCase to be instantiated without providing a metho…
Browse files Browse the repository at this point in the history
…d name.

Changed unittestgui to show number of discovered tests in the status bar.
  • Loading branch information
Michael Foord committed Jan 3, 2011
1 parent faa8c13 commit 32e1d83
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 5 deletions.
5 changes: 5 additions & 0 deletions Doc/library/unittest.rst
Original file line number Diff line number Diff line change
Expand Up @@ -721,6 +721,11 @@ Test cases
Here, we create two instances of :class:`WidgetTestCase`, each of which runs a
single test.

.. versionchanged::
`TestCase` can be instantiated successfully without providing a method
name. This makes it easier to experiment with `TestCase` from the
interactive interpreter.

*methodName* defaults to :meth:`runTest`.

:class:`TestCase` instances provide three groups of methods: one group used
Expand Down
11 changes: 8 additions & 3 deletions Lib/unittest/case.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,12 +274,17 @@ def __init__(self, methodName='runTest'):
"""
self._testMethodName = methodName
self._outcomeForDoCleanups = None
self._testMethodDoc = 'No test'
try:
testMethod = getattr(self, methodName)
except AttributeError:
raise ValueError("no such test method in %s: %s" %
(self.__class__, methodName))
self._testMethodDoc = testMethod.__doc__
if methodName != 'runTest':
# we allow instantiation with no explicit method name
# but not an *incorrect* or missing method name
raise ValueError("no such test method in %s: %s" %
(self.__class__, methodName))
else:
self._testMethodDoc = testMethod.__doc__
self._cleanups = []

# Map types to custom assertEqual functions that will compare
Expand Down
10 changes: 10 additions & 0 deletions Lib/unittest/test/test_case.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,16 @@ def test(self): pass

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

# test that TestCase can be instantiated with no args
# primarily for use at the interactive interpreter
test = unittest.TestCase()
test.assertEqual(3, 3)
with test.assertRaises(test.failureException):
test.assertEqual(3, 2)

with self.assertRaises(AttributeError):
test.run()

# "class TestCase([methodName])"
# ...
# "Each instance of TestCase will run a single test method: the
Expand Down
3 changes: 3 additions & 0 deletions Misc/NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ Core and Builtins
Library
-------

- `unittest.TestCase` can be instantiated without a method name; for simpler
exploration from the interactive interpreter.

- Issue #10798: Reject supporting concurrent.futures if the system has too
few POSIX semaphores.

Expand Down
6 changes: 4 additions & 2 deletions Tools/unittestgui/unittestgui.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,13 +276,15 @@ def settingsClicked(self):
self.test_file_glob_pattern = d.test_file_glob_pattern

def notifyTestsDiscovered(self, test_suite):
discovered = test_suite.countTestCases()
self.runCountVar.set(0)
self.failCountVar.set(0)
self.errorCountVar.set(0)
self.remainingCountVar.set(test_suite.countTestCases())
self.remainingCountVar.set(discovered)
self.progressBar.setProgressFraction(0.0)
self.errorListbox.delete(0, tk.END)
self.statusVar.set("Discovering tests from %s" % self.directory_to_read)
self.statusVar.set("Discovering tests from %s. Found: %s" %
(self.directory_to_read, discovered))
self.stopGoButton['state'] = tk.NORMAL

def createWidgets(self):
Expand Down

0 comments on commit 32e1d83

Please sign in to comment.