djt djt - 4 months ago 24
PHP Question

PHPUnit - Bash Script Output to Variable

I'm trying to capture the output of my test suite from PHPUnit to determine whether a failure occurred. However, when I attempt to store the output in a bash variable, the variable is always empty:

PHPUNIT_RESULT=`vendor/bin/phpunit`

if [ -z "$PHPUNIT_RESULT" ]; then
echo "something there!
fi


However, the variable always seems to be empty.

EDIT: Sample output

PHPUnit 3.4.5 by Sebastian Bergmann.

......F.......F

Time: 0 seconds, Memory: 8.00Mb

There was 1 failure:

1) MyTest::testTemp
Failed asserting that <boolean:false> is true.

/path/to/myTest.php:68

FAILURES!
Tests: 4, Assertions: 5, Failures: 1, Incomplete: 1.

Answer

if there's any test failure, phpunit will exit with a non-zero status. you can check this with the $? variable.

./vendor/bin/phpunit /path/to/myTest.php

if [ $? -ne 0 ]; then
        echo "failed test"
fi