Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Serhiy Storchaka Newsgroups: comp.lang.python Subject: Re: How to parameterize unittests Date: Fri, 15 Apr 2016 11:24:53 +0300 Lines: 48 Message-ID: References: <570FA466.20003@rece.vub.ac.be> <570fb1a3$0$1609$c3e8da3$5496439d@news.astraweb.com> Mime-Version: 1.0 Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 7bit X-Trace: news.uni-berlin.de rld8yjmdm3TkO4g0sDfREg09xZPPh+aSB0V8MPKOcGcQ== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'classes.': 0.07; 'unittest': 0.07; 'subject:How': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subclass': 0.09; 'def': 0.13; '2016': 0.16; 'received:80.91.229.3': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'subclasses,': 0.16; 'wrote:': 0.16; 'tree': 0.18; 'tests': 0.18; 'am,': 0.23; 'header:In-Reply-To:1': 0.24; 'header:User-Agent:1': 0.26; 'header:X-Complaints-To:1': 0.26; 'define': 0.27; 'fri,': 0.27; 'skip:t 40': 0.27; 'module.': 0.27; 'run': 0.33; 'class': 0.33; 'common': 0.33; "d'aprano": 0.33; 'largely': 0.33; 'steven': 0.33; 'instance': 0.35; 'should': 0.36; 'there': 0.36; 'to:addr :python-list': 0.36; 'subject:: ': 0.37; 'received:org': 0.37; 'test': 0.39; 'to:addr:python.org': 0.40; 'skip:u 10': 0.61; 'charset:windows-1252': 0.62; 'reuse': 0.66; 'dag': 0.84; 'pardon': 0.84; 'approach.': 0.91 X-Injected-Via-Gmane: http://gmane.org/ X-Gmane-NNTP-Posting-Host: 193.202.118.164 User-Agent: Mozilla/5.0 (X11; Linux i686; rv:38.0) Gecko/20100101 Thunderbird/38.6.0 In-Reply-To: <570fb1a3$0$1609$c3e8da3$5496439d@news.astraweb.com> X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Mailman-Original-Message-ID: X-Mailman-Original-References: <570FA466.20003@rece.vub.ac.be> <570fb1a3$0$1609$c3e8da3$5496439d@news.astraweb.com> Xref: csiph.com comp.lang.python:107030 On 14.04.16 18:05, Steven D'Aprano wrote: > On Fri, 15 Apr 2016 12:08 am, Antoon Pardon wrote: >> I have a unittest for my avltree module. >> >> Now I want this unittest to also run on a subclass of avltree. >> How can I organise this, so that I can largely reuse the >> original TestCase? > > > class Test_AVLTree(unittest.TestCase): > tree = avltree > > def test_empty_tree_is_false(self): > instance = self.tree() > self.assertFalse(instance) > > > class Test_MySubclassTree(Test_AVLTree): > tree = My_Subclass_Tree Yes, this is common approach. If there tests specific for original class or tests that there is no need to run for subclasses, you should define common tests in mixin class that is not test class itself: class AbstractTest_AVLTree: # note, there is no TestCase! def test_common(self): ... class Test_AVLTree(AbstractTest_AVLTree, unittest.TestCase): tree = avltree def test_base_class_specific(self): ... class Test_MySubclassTree(AbstractTest_AVLTree, unittest.TestCase): tree = My_Subclass_Tree def test_sub_class_specific(self): ... Complex tests can have a DAG of test classes.