Why does phpunit not show any errors in the console

27,711

Solution 1

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');

Solution 2

This is a very common issue, especially when you are running tests on a production server 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.

Share:
27,711
Nyxynyx
Author by

Nyxynyx

Hello :) I have no formal education in programming :( And I need your help! :D These days its web development: Node.js Meteor.js Python PHP Laravel Javascript / jQuery d3.js MySQL PostgreSQL MongoDB PostGIS

Updated on July 09, 2022

Comments

  • Nyxynyx
    Nyxynyx almost 2 years

    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

  • flyingL123
    flyingL123 about 6 years
    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 <php></php> element to the phpunit.xml file that comes with Laravel, but no luck. Errors are still not displayed. Any idea why not?
  • sbuck
    sbuck over 5 years
    @flyingL123 Any chance you're testing it with Dusk tests? If so, add it to phpunit.dusk.xml, not phpunit.xml
  • flyingL123
    flyingL123 over 5 years
    No not using Dusk
  • redanimalwar
    redanimalwar over 4 years
    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.
  • Victor Schröder
    Victor Schröder over 2 years
    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.