NYCPHP Meetup

NYPHP.org

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

Anthony Ferrara ircmaxell at gmail.com
Fri Nov 15 11:01:36 EST 2013


The primary reason that you don't see projects using inheritance like that
is that it's not actually good OO design to do so. And the fact that you
can't design the tests the same way as the classes just further reinforce
the fact that the abstractions are wrong.

Check out this video where I talk on it to some length:
http://www.youtube.com/watch?v=G32acYVd9LY

Stick to composition around 90% of the time. Don't rely on inheritance for
functionality. You'll wind up with far easier to maintain code...

Anthony


On Fri, Nov 15, 2013 at 10:26 AM, Gary A. Mort <garyamort at gmail.com> wrote:

> 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
> _______________________________________________
> New York PHP User Group Community Talk Mailing List
> http://lists.nyphp.org/mailman/listinfo/talk
>
> http://www.nyphp.org/show-participation
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.nyphp.org/pipermail/talk/attachments/20131115/328c3251/attachment.html>


More information about the talk mailing list