subclassing from unittest

Terry Jan Reedy tjreedy at udel.edu
Thu May 23 15:42:35 EDT 2013


On 5/23/2013 2:58 AM, Ulrich Eckhardt wrote:

> Well, per PEP 8, classes use CamelCaps, so your naming might break
> automatic test discovery. Then, there might be another thing that could
> cause this, and that is that if you have an intermediate class derived
> from unittest.TestCase, that class on its own will be considered as test
> case! If this is not what you want but you still want common
> functionality in a baseclass, create a mixin and then derive from both
> the mixin and unittest.TestCase for the actual test cases.

This is now standard practice, gradually being implemented everywhere in 
the CPython test suite, for testing C and Py versions of a module.

class TestXyz():
   mod = None
   <test_a, etc, methods>

class TestXyz_C(TestXyz, TextCase):  # Test C version
   mod = support.import_fresh_module('_xyz')  # approximately right

class TestXyz_Py(TestXyz, TextCase):  # Test Python version
   mod = support.import_fresh('xyz')

This minimizes duplication and ensures that both implementations get 
exactly the same tests.

tjr





More information about the Python-list mailing list