5

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.
djt
  • 7,297
  • 11
  • 55
  • 102
  • To start with, generally speaking, using backticks to perform command substitution in `bash` is discouraged. Use the `$(...)` syntax instead. In either case, what's the expected output of `vendor/bin/phpunit`? – Sebastian Lenartowicz Jul 24 '16 at 16:32
  • 1
    @SebastianLenartowicz thanks for the info. I've also tried it with `$()` and had the same results. I've updated the question with a sample output. It is multi-line output. – djt Jul 24 '16 at 16:35
  • 3
    have you already see [this](https://stackoverflow.com/questions/29164652/simple-check-of-phpunit-result/29165024#29165024)? – Matteo Jul 24 '16 at 16:41
  • Don't forget as well that the `-z` operator checks for an empty string - so, if you print "Something there!", it actually means you're getting output! – Sebastian Lenartowicz Jul 24 '16 at 16:48
  • 1
    @Matteo thanks for that link, it lead to some useful results – djt Jul 24 '16 at 17:18

1 Answers1

6

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
Jeff Puckett
  • 37,464
  • 17
  • 118
  • 167