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.
5
  • 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? Commented Jul 24, 2016 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. Commented Jul 24, 2016 at 16:35
  • 3
    have you already see this? Commented Jul 24, 2016 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! Commented Jul 24, 2016 at 16:48
  • 1
    @Matteo thanks for that link, it lead to some useful results Commented Jul 24, 2016 at 17:18

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for this. Thats exactly what I ended up doing

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.