Test the Test

Just a recommendation and a “best practise” coming from my current daily work on Test First Development. I wanted to introduce a new ValueObject, thus I started to write the Unit Test for it. It looked like this in my IDE (Eclipse; pseudo code used):

Bildschirmfoto vom 2014-12-17 21:43:52This is the source code:

class MyTest {
    public function checkDefaultValues() {
        $myValueObject = $this->createValueObject();
        $this->assertInstanceOf(Tx_Extbase_DomainObject_AbstractValueObject, $myValueObject, 'ValueObject does not implement AbstractValueObject');
        $this->assertABC(/*...*/);
    }

    /**
     * @test
     */
    public function checkSetAndGetPropertyA() {
        // ...
    }

    /**
     * @test
     */
    public function checkSetAndGetPropertyB() {
        // ...
    }
}

After i wrote the test like quoted above I tended to implement the ValueObject without any prior test run, because I was just very sure about what’s to be done and it was quite abvious that the test will fail very fast without any testable class in place. Fortunately I controled my feelings and had a break. I just started the test and did the implementation step by step based on the upcoming error messages. I was astonished! Why? Read on…

The very first test run failed, because no ValueObject class was available at all. That’s no rocket sience. So I did the first very, very basic implementation of “ValueObject”. It looked like this:

class ValueObject {
}

Simple. Important: I did not extend the ValueObject from the base class Tx_Extbase_DomainObject_AbstractValueObject on purpose to find out, if the “assertInstanceOf” of my test (highlighted in line #4 of the test case implementation) would fire an assertion failure. Surprise: It didn’t!

Why? Because “checkDefaultValues” was never executed at all. And that is, because I forgot to add the “@test” annotation for this test. I wasn’t aware of this bug, because the annotations were minimised in my IDE view (have a look at the screenshot on top of this post).

If I would have implemented the ValueObject in the usual way using a copy&paste of another value object or by adding the extension in the very first implementation, I wouldn’t have seen this flaw in the test.

Outcome: “Test First Development” means also: “Implementation driven by test defects.” This will test your tests and will let you have better tests and better software. The only thing on your side: You have to be disciplined… Right after this experience I won’t do it differently any more. 😀

Leave a Reply