Best strategy for testing class and subclasses in pytest?

Jean-Michel Pichavant jeanmichel at
Tue Aug 25 13:06:16 CEST 2015

> From: "C.D. Reimer" <chris at>
> Greetings,
> 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
> find
> 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
> implemented
> 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.

import pytest

class Piece(object):
	def isPiece(self): #for the sake of demo
		return True
	def name(self):
		raise NotImplementedError # Piece is a sort of abstract class

class Queen(Piece):
	def name(self):
		return 'Queen'

class TestPiece(object):
	cls = Piece
	def test_isPiece(self):
		assert self.cls().isPiece
	def test_name(self):
		with pytest.raises(NotImplementedError):
			assert self.cls().name

class TestQueen(TestPiece):
	cls = Queen
	def test_name(self):
		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


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 mailing list