Path: csiph.com!eternal-september.org!feeder.eternal-september.org!border1.nntp.ams1.giganews.com!nntp.giganews.com!newsfeed.xs4all.nl!newsfeed7.news.xs4all.nl!nzpost1.xs4all.net!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; 'correspond': 0.07; 'behave': 0.09; 'expected.': 0.09; 'functioning': 0.09; 'implies': 0.09; 'overridden': 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; 'subclasses': 0.09; 'units.': 0.09; 'python': 0.10; 'class),': 0.16; 'pytest': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'subclasses.': 0.16; 'subject:class': 0.16; 'subject:strategy': 0.16; 'subject:subclasses': 0.16; 'tested)': 0.16; 'test.': 0.18; 'tests': 0.18; 'class,': 0.22; 'module': 0.25; 'testing': 0.25; "i've": 0.25; 'header:User-Agent:1': 0.26; 'header:X-Complaints-To:1': 0.26; 'pieces': 0.27; 'tend': 0.27; 'correct': 0.28; 'sensible': 0.29; "i'm": 0.30; 'classes': 0.30; 'class.': 0.30; 'another': 0.32; 'implement': 0.32; 'functional': 0.32; 'class': 0.33; 'usually': 0.33; 'similar': 0.33; 'case,': 0.34; 'structure': 0.34; 'could': 0.35; 'i.e.': 0.35; 'unit': 0.35; 'but': 0.36; 'created': 0.36; 'modules': 0.36; 'to:addr :python-list': 0.36; 'subject:?': 0.36; 'subject:: ': 0.37; 'received:org': 0.37; 'charset:us-ascii': 0.37; 'integration': 0.38; 'test': 0.39; 'sure': 0.39; 'to:addr:python.org': 0.40; 'received:de': 0.40; 'your': 0.60; 'avoid': 0.61; 'engine': 0.62; 'inherit': 0.66; 'received:217': 0.66; 'potentially': 0.67; 'services': 0.67; 'strategy': 0.69; 'asume': 0.84; 'route': 0.84; 'verifying': 0.84; 'inheritance,': 0.93; 'subject:Best': 0.93 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: dieter Subject: Re: Best strategy for testing class and subclasses in pytest? Date: Mon, 24 Aug 2015 07:32:12 +0200 References: <55D8CE06.7030206@cdreimer.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Gmane-NNTP-Posting-Host: pd9e09c75.dip0.t-ipconnect.de User-Agent: Gnus/5.1008 (Gnus v5.10.8) XEmacs/21.4.22 (linux) Cancel-Lock: sha1:815vSGpukC9TssIyJYGBPMCUY+g= X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ 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: 29 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1440394346 news.xs4all.nl 23748 [2001:888:2000:d::a6]:58479 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:95606 "C.D. Reimer" writes: > I'm writing a chess engine to learn about Python classes and inheritance, and using pytest for the unit test. I've created a Piece class, which has 99% of the functionality for a chess piece, and subclass the other pieces -- Bishop, King, Knight, Pawn, Queen, Rook -- > that will implement the move-specific functionality. I'm not sure > what's the best strategy is for testing the class and subclasses. I tend to give the tests a structure similar to the implementation, i.e. the test units correspond to the functional units. In your case, I would have tests verifying the correct functioning of the base class and tests verifying that of the subclasses. When an integration module uses the (already tested) services of another module (as your subclasses use the services of the base class), I try to avoid retesting the other modules functionality - but asume in my new tests that the other (used) modules behave as expected. This often significantly reduces the test complexity. I your setup with base class and subclasses this implies that I usually test only new and overridden methods in the subclasses. In your case, you could also inherit your subclasses' test case classes from that of your base class. This would lead to a reexecution of the base class tests as part of the tests for each subclass. I follow this route when a subclass has overridden base class methods in a potentially sensible way.