Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'python': 0.09; "'requires": 0.09; 'defined,': 0.09; 'modules.': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'terry': 0.09; 'def': 0.10; 'benjamin': 0.16; 'cls': 0.16; 'hierarchy': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'reedy': 0.16; 'skip:@ 20': 0.16; 'subject:class': 0.16; 'wrote:': 0.17; 'instance,': 0.17; 'jan': 0.18; 'tests': 0.18; 'module': 0.19; 'code.': 0.20; 'import': 0.21; 'regardless': 0.21; 'class.': 0.23; 'this:': 0.23; 'testing': 0.24; 'pass': 0.25; 'header:In- Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'skip:[ 10': 0.26; 'am,': 0.27; 'correct': 0.28; 'header:X-Complaints-To:1': 0.28; 'run': 0.28; 'coded': 0.29; "d'aprano": 0.29; 'omitted': 0.29; 'steven': 0.29; 'class': 0.29; 'classes': 0.30; 'version,': 0.30; 'skip:s 30': 0.33; 'exhibit': 0.33; 'to:addr:python-list': 0.33; 'skip:d 20': 0.34; 'list': 0.35; 'needed': 0.35; "won't": 0.35; 'received:org': 0.36; 'but': 0.36; 'wanted': 0.36; 'test': 0.36; 'itself': 0.37; 'october': 0.37; 'skip:t 40': 0.37; 'two': 0.37; 'subject:: ': 0.38; 'to:addr:python.org': 0.39; 'where': 0.40; 'header:Received:5': 0.40; 'your': 0.60; 'oscar': 0.84; 'received:fios.verizon.net': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Terry Reedy Subject: Re: unit testing class hierarchies Date: Wed, 03 Oct 2012 16:14:39 -0400 References: <506b92f3$0$29982$c3e8da3$5496439d@news.astraweb.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: pool-173-75-251-66.phlapa.fios.verizon.net User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:15.0) Gecko/20120824 Thunderbird/15.0 In-Reply-To: X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 63 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1349402456 news.xs4all.nl 6845 [2001:888:2000:d::a6]:51931 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:30772 On 10/3/2012 5:33 AM, Oscar Benjamin wrote: > On 3 October 2012 02:20, Steven D'Aprano > 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 ... 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