]> granicus.if.org Git - python/commitdiff
Enable unittest.TestCase to be instantiated without providing a method name.
authorMichael Foord <fuzzyman@voidspace.org.uk>
Mon, 3 Jan 2011 17:00:11 +0000 (17:00 +0000)
committerMichael Foord <fuzzyman@voidspace.org.uk>
Mon, 3 Jan 2011 17:00:11 +0000 (17:00 +0000)
Changed unittestgui to show number of discovered tests in the status bar.

Doc/library/unittest.rst
Lib/unittest/case.py
Lib/unittest/test/test_case.py
Misc/NEWS
Tools/unittestgui/unittestgui.py

index ad6d31405e5e71ff04d7078f211a70837a5d1704..564ff72b7dce7ba4e6520f2ac2c450eac5fb0480 100644 (file)
@@ -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
index 004a9f5d33620450d0281b0866b28613a2fee2e6..0277ac861cfd483d286a98d64a6cf4c109b4ab6b 100644 (file)
@@ -274,12 +274,17 @@ class TestCase(object):
         """
         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
index 9a09dfaff0617283ed8b55a1a9df606ffc404e7f..1513fbecb72791484ac3e1f91441ac813ffbf1be 100644 (file)
@@ -77,6 +77,16 @@ class Test_TestCase(unittest.TestCase, TestEquality, TestHashing):
 
         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
index ec870e7c083f6da52c8c4c8fc685d29a8e4cb56c..930b47ed9c6a6b6b80e9825d882cd6fa87ef7bc1 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -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.
 
index 0c48b49c1bc639eedec98b5d5338d60ab5e8dcbb..b526646b06bc9e5e3ca71427ae0da9a5786dff51 100644 (file)
@@ -276,13 +276,15 @@ class TkTestRunner(BaseGUITestRunner):
         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):