Path: csiph.com!usenet.pasdenom.info!dedibox.gegeweb.org!gegeweb.eu!nntpfeed.proxad.net!proxad.net!feeder1-2.proxad.net!usenet-fr.net!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!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.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'unittest': 0.07; 'python': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'def': 0.10; 'eckhardt': 0.16; 'esse': 0.16; 'oct': 0.16; 'received:80.91.229.3': 0.16; 'received:dip.t-dialin.net': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-dialin.net': 0.16; 'redundant,': 0.16; 'subject:class': 0.16; 'wrote:': 0.17; 'instance': 0.17; 'tests.': 0.17; 'tests': 0.18; '>>>': 0.18; 'import': 0.21; 'sorry,': 0.22; 'runs': 0.22; 'idea': 0.24; 'pass': 0.25; 'header:User-Agent:1': 0.26; 'creating': 0.26; 'see,': 0.27; 'header:X-Complaints-To:1': 0.28; 'run': 0.28; 'cat': 0.29; 'class': 0.29; 'classes': 0.30; 'basic': 0.30; 'code': 0.31; 'could': 0.32; '+0200,': 0.33; 'instances': 0.33; 'to:addr:python-list': 0.33; 'there': 0.35; 'received:org': 0.36; 'created': 0.36; 'possible': 0.37; 'subject:: ': 0.38; 'to:addr:python.org': 0.39; 'header:Received:5': 0.40; 'skip:u 10': 0.60; 'thomas': 0.62; 'provide': 0.62; 'more': 0.63; 'today,': 0.64; 'intentions': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Peter Otten <__peter__@web.de> Subject: Re: unit testing class hierarchies Date: Tue, 02 Oct 2012 19:38:23 +0200 Organization: None References: Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Gmane-NNTP-Posting-Host: p5084a441.dip.t-dialin.net User-Agent: KNode/4.7.3 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: 71 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1349199551 news.xs4all.nl 6911 [2001:888:2000:d::a6]:52620 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:30647 Ulrich Eckhardt wrote: > Am 02.10.2012 16:06, schrieb Thomas Bach: >> On Tue, Oct 02, 2012 at 02:27:11PM +0200, Ulrich Eckhardt wrote: >>> As you see, the code for test_base() is redundant, so the idea is to >>> move it to a baseclass: >>> >>> class TestBase(unittest.TestCase): >>> def test_base(self): >>> ... >>> >>> class TestD1(TestBase): >>> def test_r(self): >>> ... >>> def test_s(self): >>> ... >>> >>> class TestD2(TestBase): >>> def test_x(self): >>> ... >>> def test_y(self): >>> ... >> >> Could you provide more background? How do you avoid that test_base() >> runs in TestD1 or TestD2? > > Sorry, there's a misunderstanding: I want test_base() to be run as part > of both TestD1 and TestD2, because it tests basic functions provided by > both classes D1 and D2. The instances of D1 and D2 are created in > TestD1.setUp and TestD2.setUp and then used by all tests. There is no > possible implementation creating such an instance for TestBase, since > the baseclass is abstract. > > Last edit for today, I hope that makes my intentions clear... > > ;) Ceterum censeo baseclassinem esse delendam ;) $ cat test_shared.py import unittest class Shared(unittest.TestCase): def test_shared(self): pass class D1(Shared): def test_d1_only(self): pass class D2(Shared): def test_d2_only(self): pass del Shared unittest.main() $ python test_shared.py -v test_d1_only (__main__.D1) ... ok test_shared (__main__.D1) ... ok test_d2_only (__main__.D2) ... ok test_shared (__main__.D2) ... ok ---------------------------------------------------------------------- Ran 4 tests in 0.000s OK $