Why does phpunit not show any errors in the console
Asked Answered
F

4

47

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

Farrow answered 4/8, 2013 at 21:40 Comment(1)
You can get all PHPunit errors. [This one][1] or [This one][2] will be useful for you. [1]: https://mcmap.net/q/372423/-phpunit-errors [2]: #5995229Proponent
C
33

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');
Carlsbad answered 8/12, 2013 at 7:50 Comment(0)
M
58

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.

Mis answered 7/7, 2015 at 21:52 Comment(6)
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?Devotee
@Devotee Any chance you're testing it with Dusk tests? If so, add it to phpunit.dusk.xml, not phpunit.xmlObeisance
No not using DuskDevotee
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.Replenish
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.Recently
@Replenish this problem is driving me mad. Please if anyone has any answers that work, I created this question: #76207498Ja
C
33

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');
Carlsbad answered 8/12, 2013 at 7:50 Comment(0)
A
0

In my case I had all the options set, and yet all the console showed was error 255. It turned out that in one of the test files I had syntax not allowed for this version of php. After adjusting this part, in my case, the nullsafe operator available from 8, the tests started working (I was checking compatibility for lower version of app which runs on 7.4)

Acosmism answered 26/12, 2023 at 20:21 Comment(0)
D
0

You should make sure that you have enabled this parameter in your phpunit.xml config:

displayDetailsOnTestsThatTriggerWarnings="true"

Alternatively, you can enabled just for one run by passing along this flag to phpunit CLI: --display-warnings

Dominickdominie answered 8/1 at 10:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.