http://www.perlmonks.org?node_id=294571

We had a strange debate at work today. Basically, the question was whether or not it was appropriate to inherit tests. I argued that it was. If I write a subclass, I don't want to rewrite all of the tests. I just want to test what's in the subclass and possibly inherit the tests from the superclass.

The counter-argument, provided by another programmer, is that by failing to explicitly test superclass behavior in the subclass tests, we're losing information. For example, let's say I have a superclass that, amongst other things, formats numbers to have a comma every three digits. My subclasses might change some behavior, but in this example, never change the number format behavior and thus have no tests for it. The problem arises when the super class number formatter is changed to have the comma every four digits, but the subclasses don't have tests for this and thus we're altering subclass behavior without testing it.

Personally, if I have to change a super class, I don't want to find everything that might subclass from it and update all of the tests. I want to run the tests and just fix the ones that break. Copying superclass tests over and over to subclass tests strikes me as a bad idea, but I have to admit that the counter-argument was interesting.

Have you ever used Test::Class or something similar to subclass tests? Do you think it's a good or bad idea? I'm also curious about whether or not you think that good software design considerations are different for tests. Frankly, I see a lot of test code that appears poorly written (cut and paste, mostly), but even bad tests are better than no tests, right?

Cheers,
Ovid

New address of my CGI Course.