Skip to content

Instantly share code, notes, and snippets.

@rtablada
Created February 17, 2014 02:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rtablada/9043781 to your computer and use it in GitHub Desktop.
Save rtablada/9043781 to your computer and use it in GitHub Desktop.
Do you test all the child classes of an abstract class?

So if I have a parent abstract class that is fully implemented except for the defining property number:

abstract class DividesByFive
{
	protected $number;

	public function getResult()
	{
		return $this->number / 5;
	}
}

When going to test things, I have multiple child classes based on the parent class.

I test the getResult in my SevenDividedByFive class. Do you guys test the getResult method for each of the implementations even though it is covered in our SevenDividedByFive test? Or do you just add tests for features that may differ in new children classes?

@adamwathan
Copy link

I would not even test that in the SevenDividedByFive class. Instead I would create a child class directly in the test file that inherits from DividesByFives and exists ONLY to be the single source where the methods actually implemented in DividesByFive are tested, and only because of the technicality that you can't instantiate the abstract class directly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment