NYCPHP Meetup

NYPHP.org

[nycphp-talk] Why do unit tests not inherit?

Gary A. Mort garyamort at gmail.com
Fri Nov 15 10:26:56 EST 2013


Is there a reason that most unit tests published for PHP Open Source 
projects do not use OOP programming?

--
Class Pets
Class Dogs extends Pets
Class Cats extends Pets


Unit tests written for those classes generally are defined like
Class TestPets extends TestCase
Class TestDogs extends TestCase
Class TestCats extends TestCase


It seems to me that it should be
Class TestPets extends TestCase
Class TestDogs extends TestPets
Class TestCats extends TestPets

That way if the Pets base class is given a new method based on a method 
in Dogs but with subtle differences, by inheriting the TestPets unit 
tests you will immediately find out that the Dogs class is failing the 
Pets unit tests and either needs to be changed to match or if it is 
supposed to be different, the test from TestPets has to be disabled in 
TestDogs.

I'm trying to wrap my brain around this and wondered if there is some 
valid reason for not using inheritance for tests in this way - or if it 
is just that for whatever reason when programmers first started writing 
unit tests they got stuck in a functional rather than object oriented 
mindset?

-Gary


More information about the talk mailing list