为什么phpunit在控制台中没有显示任何错误

时间:2021-09-24 02:35:34

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)?

我正在使用phpunit和Laravel 4框架。为什么在测试期间出现PHP错误时,不会显示错误消息(例如:缺少方法)?

How can we get phpunit to show all errors?

我们怎样才能让phpunit显示所有错误?

为什么phpunit在控制台中没有显示任何错误

2 个解决方案

#1


27  

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

我认为问题可能是指PHP本身,而不是PHPUnit。请遵循以下步骤:

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

1.检查正确的php.ini。请注意,某些系统可以为不同的PHP SAPI使用不同的php.ini:

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:

2.编辑错误输出设置。在相应的php.ini中为error_reporting,display_errors,display_startup_errors设置适当的设置:

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.

如果您不想在全局范围内更改CLI错误报告行为,则可以使用PHPUnit引导文件来定义这些设置。

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

1.为PHPUnit设置bootstrap。打开/Applications/MAMP/htdocs/testtingDecoded/phpunit.xml文件并将bootstrap属性添加到phpunit标记:

<phpunit bootstrap="bootstrap.php">

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

2.使用phpunit.xml在文件夹中创建bootstrap.php:

<?php
ini_set('error_reporting', E_ALL); // or error_reporting(E_ALL);
ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');

#2


42  

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.

这是一个非常常见的问题,尤其是当您在生产服务器上运行测试或者测试人员不太了解PHP配置时。

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

这个问题与php.ini设置有关,正如Alexander Yancharuk在他的回答中指出的那样,他建议的所有解决方案都可以正常工作。

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配置文件(XML)本身中设置适当的PHP设置,如下所示:

<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.

使用此功能,您不仅可以个性化错误显示,还可以个性化许多PHP配置,特别是对于您的测试套件,使您的生产配置保持不变,而无需为此编写引导文件。

#1


27  

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

我认为问题可能是指PHP本身,而不是PHPUnit。请遵循以下步骤:

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

1.检查正确的php.ini。请注意,某些系统可以为不同的PHP SAPI使用不同的php.ini:

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:

2.编辑错误输出设置。在相应的php.ini中为error_reporting,display_errors,display_startup_errors设置适当的设置:

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.

如果您不想在全局范围内更改CLI错误报告行为,则可以使用PHPUnit引导文件来定义这些设置。

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

1.为PHPUnit设置bootstrap。打开/Applications/MAMP/htdocs/testtingDecoded/phpunit.xml文件并将bootstrap属性添加到phpunit标记:

<phpunit bootstrap="bootstrap.php">

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

2.使用phpunit.xml在文件夹中创建bootstrap.php:

<?php
ini_set('error_reporting', E_ALL); // or error_reporting(E_ALL);
ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');

#2


42  

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.

这是一个非常常见的问题,尤其是当您在生产服务器上运行测试或者测试人员不太了解PHP配置时。

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

这个问题与php.ini设置有关,正如Alexander Yancharuk在他的回答中指出的那样,他建议的所有解决方案都可以正常工作。

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配置文件(XML)本身中设置适当的PHP设置,如下所示:

<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.

使用此功能,您不仅可以个性化错误显示,还可以个性化许多PHP配置,特别是对于您的测试套件,使您的生产配置保持不变,而无需为此编写引导文件。