On 10/3/2012 5:33 AM, Oscar Benjamin wrote:
On 3 October 2012 02:20, Steven D'Aprano
<steve+comp.lang.pyt...@pearwood.info> wrote:

But surely, regardless of where that functionality is defined, you still
need to test that both D1 and D2 exhibit the correct behaviour? Otherwise
D2 (say) may break that functionality and your tests won't notice.

Given a class hierarchy like this:

class AbstractBaseClass:
     spam = "spam"

class D1(AbstractBaseClass): pass
class D2(D1): pass


I write tests like this:

class TestD1CommonBehaviour(unittest.TestCase):
     cls = D1
     def testSpam(self):
          self.assertTrue(self.cls.spam == "spam")
     def testHam(self):
          self.assertFalse(hasattr(self.cls, 'ham'))

class TestD2CommonBehaviour(TestD1CommonBehaviour):
     cls = D2

That's an excellent idea. I wanted a convenient way to run the same
tests on two classes in order to test both a pure python and a
cython-accelerator module implementation of the same class.

Python itself has same issue with testing Python and C coded modules. It has the additional issue that the Python class by default import the C version, so additional work is needed to avoid that and actually test the python code.

For instance, heapq.test_heapq.py has

...
py_heapq = support.import_fresh_module('heapq', blocked=['_heapq'])
c_heapq = support.import_fresh_module('heapq', fresh=['_heapq'])
...
class TestHeap(TestCase):
    module = None
... <multiple test methods for functions module.xxx>

class TestHeapPython(TestHeap):
    module = py_heapq

@skipUnless(c_heapq, 'requires _heapq')
class TestHeapC(TestHeap):
    module = c_heapq
...
def test_main(verbose=None):
    test_classes = [TestModules, TestHeapPython, TestHeapC,

# TestHeap is omitted from the list and not run directly

--
Terry Jan Reedy

--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to