46

I'm using phpunit with Laravel 4 framework. Why is it that when there's a PHP error during the tests, no error messages are shown (eg: missing method)?

How can we get phpunit to show all errors?

enter image description here

Nyxynyx
  • 61,411
  • 155
  • 482
  • 830
  • You can get all PHPunit errors. [This one][1] or [This one][2] will be useful for you. [1]: http://stackoverflow.com/questions/3569862/phpunit-errors [2]: http://stackoverflow.com/questions/5994729/phpunit-not-showing-a-stack-trace-for-a-php-fatal-error –  Oct 23 '13 at 09:19

2 Answers2

57

This is a very common issue, especially when you are running tests on a host configured like a production server (usually less verbose) or when the tester isn't very aware of the PHP configuration.

The issue is related to php.ini settings, as pointed by Alexander Yancharuk in his answer and all the solutions he suggests work fine.

But there is another solution that may be useful, as it was for me, which is to set the appropriate PHP settings in the PHPUnit configuration file (XML) itself, as follows:

<phpunit>
    <suites>
        ...
    </suites>
    <php>
        <ini name="display_errors" value="On" />
        <ini name="display_startup_errors" value="On" />
    </php>
</phpunit>

Using this you can personalize not only error display, but a lot of PHP configuration, specifically for your test suite, leaving your production configuration untouched and without having to write a bootstrap file only for this.

Victor Schröder
  • 6,738
  • 2
  • 42
  • 45
  • 7
    I tried this but it's not working for me. Just wondering if I'm missing something. I'm using Laravel 5.6 on a Mac running High Sierra. PHP version PHP 7.1.7 and phpunit 7.0.0. I added the two lines you showed within the `` element to the `phpunit.xml` file that comes with Laravel, but no luck. Errors are still not displayed. Any idea why not? – flyingL123 Mar 01 '18 at 01:56
  • @flyingL123 Any chance you're testing it with Dusk tests? If so, add it to phpunit.dusk.xml, not phpunit.xml – sbuck Oct 23 '18 at 00:25
  • No not using Dusk – flyingL123 Oct 23 '18 at 00:26
  • 3
    I also remember running into the same problem b4 without a finding a solution wasting my time. Its is beyond me why PHP Unit not shows normal errors by default and overwrites whatever is set to display errors. The fact that is the *de facto* standard testing lib for PHP does not change anything about that. It makes no sense to aggressively hide errors in a lib that is specifically made for debugging. What makes it worse this answers do not work. – redanimalwar Sep 20 '19 at 07:39
  • PHPUnit is _NOT_ made for debugging. Debuggers, such as Xdebug, are. Hiding log messages is very much necessary to have a nice _testing_ result report. Also, they are the configuration values that (should) be used in a production environment. Displaying logs while testing is an anomaly, but you can always turn them on temporarily if you have to. _"these answers do not work"_ is a false statement. They do work. Please double-check what _you_ are doing. – Victor Schröder Oct 05 '21 at 14:58
  • @redanimalwar this problem is driving me mad. Please if anyone has any answers that work, I created this question: https://stackoverflow.com/questions/76206998/how-to-make-phpunit-output-errors – Jodes May 09 '23 at 08:10
32

I think the problem is probably refers to a PHP itself, not PHPUnit. Follow this steps:

1. Check proper php.ini. Note that some systems can use different php.ini for different PHP SAPI:

php -i | grep php.ini
Configuration File (php.ini) Path => /etc/php5/cli
Loaded Configuration File => /etc/php5/cli/php.ini

2. Edit error output settings. Set appropriate settings for error_reporting, display_errors, display_startup_errors in corresponding php.ini:

error_reporting = E_ALL
display_errors = On
display_startup_errors = On

If you don't want to change CLI error reporting behavior in global scope, you can use PHPUnit bootstrap file for define those settnigs.

1. Setup bootstrap for PHPUnit. Open /Applications/MAMP/htdocs/testtingDecoded/phpunit.xml file and add bootstrap attribute to phpunit tag:

<phpunit bootstrap="bootstrap.php">

2. Create bootstrap.php in folder with phpunit.xml:

<?php
ini_set('error_reporting', E_ALL); // or error_reporting(E_ALL);
ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');
Alexander Yancharuk
  • 13,817
  • 5
  • 55
  • 55