Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHPUnit: force display of asserted values

When in PHPUnit test fails, actual and expected values are displayed.
But when the test passes, this information is not displayed.

How to force PHPUnit to always display expected and actual assertion result?

like image 957
takeshin Avatar asked Mar 17 '10 14:03

takeshin


People also ask

What is assert in PHPUnit?

The assertEquals() function is a builtin function in PHPUnit and is used to assert whether the actual obtained value is equals to expected value or not. This assertion will return true in the case if the expected value is the same as the actual value else returns false.

What is a PHPUnit test?

PHPUnit is a unit testing framework for the PHP programming language. It is an instance of the xUnit architecture for unit testing frameworks that originated with SUnit and became popular with JUnit. PHPUnit was created by Sebastian Bergmann and its development is hosted on GitHub.

What are PHPUnit fixtures?

In Testing array operations with PHPUnit, the fixture was the array that is stored in the $stack variable. Most of the time, though, the fixture will be more complex than a simple array, and the amount of code needed to set it up will grow accordingly.

How do I run a PHPUnit test case?

How to Run Tests in PHPUnit. You can run all the tests in a directory using the PHPUnit binary installed in your vendor folder. You can also run a single test by providing the path to the test file. You use the --verbose flag to get more information on the test status.


4 Answers

running

phpunit --testdox

will show each test name. So as a workaround, you could maybe incorporate your expected and actual assertion results inside the test name ... still it's just a workaround ...

like image 59
Dominik Avatar answered Oct 06 '22 00:10

Dominik


Since you're most likely calling the assertions with $this->assert...(), you can just overwrite those methods in your test case. Quick example:

class YourTestCase extends PHPUnit_Framework_TestCase {
    ...
    static private $messages = array();
    ...
    static public function assertSame($var1, $var2, $message = '') {
        parent::assertSame($var1, $var2, $message);
        // assertSame() throws an exception if not true, so the following
        // won't occur unless the messages actually are the same
        $success = print_r($var1, true) . ' is the same as '
                 . print_r($var2, true);
        self::$messages = array_merge(self::$messages, array($success));
    }

    static public function tearDownAfterClass() {
        echo implode("\n", self::$messages);
    }
}

Of course, tearDownAfterClass() may not be late enough for your liking. It's not the same as an assertion failure would be.

like image 20
pinkgothic Avatar answered Oct 05 '22 23:10

pinkgothic


I came to this post looking for something similar. I have this testcase:

/**
 * test routing logic (numbers method returns an array of numbers and expected outputs to test)
 * @dataProvider numbers
 */
function testRoute($input,$expected)
{
   $route = new Route($input,'',false);
   $route->route();
   $this->assertEquals($expected,$route->routingResult);
}

and my numbers method is this:

/**
 * read pairs of numbers (input <tab> expected) from tests.input separater by tab
 * return an array like this: array(array(number1,expected1), array(number2,expected2), ...)
 * provide this array to my tests by returning it
 */
function numbers()
{
    $testcases = file('tests.input');
    $tests = array();
    foreach($testcases as $test_case)
    {
        list($input,$output) = explode("\t",$test_case,2);
        $tests[] = array(trim($input),trim($output));
    }
    return $tests;
}

What happens is you get an output like this from phpunit:

 Starting test 'RouteTest::testRoute with data set #0 ('8596000000', 'rejected (dp not found)x')'.
 F
 Starting test 'RouteTest::testRoute with data set #1 ('8596000001', 'rejected (rejected by scheme)')'.
 .
 Starting test 'RouteTest::testRoute with data set #2 ('8596000003', '1599000003')'.
 .

It won't tell you the actual result of the tested function unless the test fails but at least you get to see all the asserted values.

like image 26
amne Avatar answered Oct 05 '22 23:10

amne


Either create your own Assertion class and have it behave like a proxy to the actual assertion class and echoing the values before delegating to the actual assertion, e.g.

$this->assertWithLogging('assertion', $expected, $actual, $message);

or override PHPUnit's own class (which I think will be very tricky) or simply do

$this->assertSame($expected, $actual, $message);
echo "$expected is $actual";

That's not pretty either, because it will screw up output when running through CLI. If you happen to use Zend Studio, you will see the output in the Debug Output Tab.

Another route would be with TestListeners, but I don't know enough about them to tell you any details. Looks like you can hook into the testing process.

like image 22
Gordon Avatar answered Oct 06 '22 00:10

Gordon