Best strategy for testing class and subclasses in pytest?
jeanmichel at sequans.com
Tue Aug 25 13:06:16 CEST 2015
> From: "C.D. Reimer" <chris at cdreimer.com>
> I'm writing a chess engine to learn about Python classes and
> inheritance, and using pytest for the unit test.
> I tried to create a separate class and/or module to import the common
> tests for each class and subclass. My attempts often ended in failure
> with the "RuntimeError: super(): no arguments" message. I couldn't
> a working example on the Internet on how to do that. The pytest
> documentation is all over the place.
> Is there a way to reuse tests in pytest?
> Or should I test everything in the class and test only the
> functionality in the subclasses?
> Thank you,
> Chris R.
I've played a little bit with pytest, I was interested in trying since it claims to add less boilerplate than unittest.
I've created 2 classes, Piece and Queen, both have the 'isPiece' and 'name' property (for the sake of demo).
If you execute the code (python 2.7) with pytest, you'll see that the TestQueen class actually execute 2 tests, one inherited from its base test class TestPiece.
So in the end, I'd say that you may put all common tests in TestPiece, and each specific implementation into TestQueen.
def isPiece(self): #for the sake of demo
raise NotImplementedError # Piece is a sort of abstract class
cls = Piece
cls = Queen
assert self.cls().name == 'Queen'
platform linux2 -- Python 2.7.3 -- py-1.4.20 -- pytest-2.5.2
4 passed in 0.01 seconds
-- IMPORTANT NOTICE:
The contents of this email and any attachments are confidential and may also be privileged. If you are not the intended recipient, please notify the sender immediately and do not disclose the contents to any other person, use it for any purpose, or store or copy the information in any medium. Thank you.
More information about the Python-list