[Python-checkins] peps: #16835: update PEP 399 idiom to make it compatible with unittest test discovery.

ezio.melotti python-checkins at python.org
Fri Jan 4 21:24:02 CET 2013


http://hg.python.org/peps/rev/3740f42d3b94
changeset:   4650:3740f42d3b94
user:        Ezio Melotti <ezio.melotti at gmail.com>
date:        Fri Jan 04 22:23:50 2013 +0200
summary:
  #16835: update PEP 399 idiom to make it compatible with unittest test discovery.

files:
  pep-0399.txt |  37 +++++++++++++++++++------------------
  1 files changed, 19 insertions(+), 18 deletions(-)


diff --git a/pep-0399.txt b/pep-0399.txt
--- a/pep-0399.txt
+++ b/pep-0399.txt
@@ -8,7 +8,7 @@
 Content-Type: text/x-rst
 Created: 04-Apr-2011
 Python-Version: 3.3
-Post-History: 04-Apr-2011, 12-Apr-2011, 17-Jul-2011, 15-Aug-2011
+Post-History: 04-Apr-2011, 12-Apr-2011, 17-Jul-2011, 15-Aug-2011, 01-Jan-2013
 
 Abstract
 ========
@@ -124,7 +124,7 @@
     py_heapq = import_fresh_module('heapq', blocked=['_heapq'])
 
 
-    class ExampleTest(unittest.TestCase):
+    class ExampleTest:
 
         def test_heappop_exc_for_non_MutableSequence(self):
             # Raise TypeError when heap is not a
@@ -136,33 +136,34 @@
                     return 0
 
             heap = Spam()
-            self.assertFalse(isinstance(heap,
-                                collections.abc.MutableSequence))
+            self.assertIsInstance(heap, collections.abc.MutableSequence)
             with self.assertRaises(TypeError):
                 self.heapq.heappop(heap)
 
 
-    class AcceleratedExampleTest(ExampleTest):
+    class PyExampleTest(ExampleTest, unittest.TestCase):
+        """Test with just the pure Python code."""
+        heapq = py_heapq
 
+
+    @unittest.skipUnless(c_heapq, 'requires the C _heapq module')
+    class CExampleTest(ExampleTest, unittest.TestCase):
         """Test using the accelerated code."""
-
         heapq = c_heapq
 
 
-    class PyExampleTest(ExampleTest):
+    if __name__ == '__main__':
+        unittest.main()
 
-        """Test with just the pure Python code."""
 
-        heapq = py_heapq
-
-
-    def test_main():
-        run_unittest(AcceleratedExampleTest, PyExampleTest)
-
-
-    if __name__ == '__main__':
-        test_main()
-
+The test module defines a base class (``ExampleTest``) with test methods
+that access the ``heapq`` module through a ``self.heapq`` class attribute,
+and two subclasses that set this attribute to either the Python or the C
+version of the module.  Note that only the two subclasses inherit from
+``unittest.TestCase`` -- this prevents the ``ExampleTest`` class from
+being detected as a ``TestCase`` subclass by ``unittest`` test discovery.
+A ``skipUnless`` decorator can be added to the class that tests the C code
+in order to have these tests skipped when the C module is not available.
 
 If this test were to provide extensive coverage for
 ``heapq.heappop()`` in the pure Python implementation then the

-- 
Repository URL: http://hg.python.org/peps


More information about the Python-checkins mailing list