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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18047844/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 16:51:57  来源:igfitidea点击:

Why does phpunit not show any errors in the console

phptddphpunitlaravellaravel-4

提问by Nyxynyx

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

我在 Laravel 4 框架中使用 phpunit。为什么在测试过程中出现 PHP 错误时,没有显示错误消息(例如:缺少方法)?

How can we get phpunit to show all errors?

我们如何让 phpunit 显示所有错误?

enter image description here

在此处输入图片说明

采纳答案by Alexander Yancharuk

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.inifor different PHP SAPI:

1. 检查正确php.ini。请注意,某些系统可以php.ini针对不同的 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_errorsin corresponding php.ini:

2. 编辑错误输出设置。对于设置适当的设置的error_reportingdisplay_errors设置display_startup_errors相应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.

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

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

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

<phpunit bootstrap="bootstrap.php">

2. Create bootstrap.phpin folder with phpunit.xml:

2.在文件夹中创建 bootstrap.phpphpunit.xml

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

回答by Victor Schr?der

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.inisettings, as pointed by Alexander Yancharukin 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 配置,专门针对您的测试套件,使您的生产配置保持不变,而不必为此编写引导程序文件。

回答by Debashis Prusty

You can use something like below to exclusively print errors to console.

您可以使用类似下面的内容专门将错误打印到控制台。

$response = $this->get('https://stackoverflow.com/questions/18047844-NotFound/');
$response->dumpSession($keys = ['error']);