Often times in unit tests I see people testing very simple things that realistically couldn’t fail. For example, given the following class:
class Foo
{
public $var = 'default val1';
public $var2 = 4;
public $var3;
public function __construct($var3)
{
$this->var3 = $var3;
}
}
Pretty simple. $var and $var2 have defaults, and $var3 is initialized through the constructor.
To some, though, this needs 3 tests. Two to check the defaults are initialized, and a third to check $var3 is assigned through the constructor. To me this seems like a waste – it seems like I’m testing the language’s implementation of these features.
Is doing test like these a good idea? If so, why?
It really isn’t worth writing tests to test the compiler, and generally not for really simple code. Not unless you have some sort of external requirement.
However, that doesn’t protect against future changes to
Foo. If I were writing some other function that relied on that behavior, I’d be very tempted to write a quick test in the tests for that function.