Question

I might be being thick here, but I can't figure out how to get a little info about why a test failed.

E.g. I have this test which is there to check that we haven't got any new methods with no test...

//TEST ALL METHODS TESTED
public function testAllMethodsTested()
{
    $temp = $this->class_namespace.substr(__CLASS__, 0, -5);
    $class_methods = get_class_methods($temp);
    $test_methods = get_class_methods(__CLASS__);

    foreach($class_methods as $class_method) 
    {
        $this->assertEquals(true, in_array('test_'.$class_method, $test_methods));
    }
}

When this fails, I get something like...

1) c_functions_core_ext_Test::testAllMethodsTested
Failed asserting that false matches expected true.

To make it easier to see where to go and fix some code, I would like to get some kind of helpful debug output in console and report.html (like you get for acceptance tests) along the lines of...

1) some_class_Test::testAllMethodsTested
Checking test exists for some_class::someMethod
Failed asserting that false matches expected true.

Is this possible for unit tests?

Was it helpful?

Solution

Yes, I am thick. It seems the assert functions allow for specifying your own message...

//TEST ALL METHODS TESTED
public function testAllMethodsTested()
{
    $target_class = $this->class_namespace.substr(__CLASS__, 0, -5);
    $class_methods = get_class_methods($target_class);
    $test_methods = get_class_methods(__CLASS__);

    $result = in_array('test_'.$class_method, $test_methods);

    //FAIL WITH A USEFUL ERROR
    $this->assertTrue($result, 'There is no test for '.$target_class.'::'.$class_method);
}

When this fails, I now get something like...

1) c_functions_core_ext_Test::testAllMethodsTested
There is no test for Namespace\target_class::someMethodName
Failed asserting that false matches expected true.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top