忽略PHPUnit中的PHP警告

忽略PHPUnit中的PHP警告

本文介绍了忽略PHPUnit中的PHP警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在代码中出现任何警告时,我都使用PHPUnit对我的函数进行单元测试,因此不会对该函数执行测试脚本,谁能告诉我如何忽略警告并继续进行测试

Am using PHPUnit for unit testing my functions when ever any warning comes in code the test script will not be executed for that functions, can anyone tell me how to ignore the warnings and proceed with testing

推荐答案

如Juhana所言,您首先应修复代码,其中出现警告.这表明该代码无法正常/严格地运行.

As Juhana commented you should first of all fix your code where the warning(s) appear. It's a sign that the code is not working properly / strictly.

请参见测试PHP错误,其中提供了更多有关如何测试警告(以及如何在测试中调用的子例程中忽略警告)的信息.

See Testing PHP Errors which has more information how to test for your warnings (and how to ignore warnings in sub-routines you call in tests).

要禁用默认行为,您可以在测试中告诉PHPUnit这样做,例如在测试的setUp或测试本身中,方法是在全局名称空间中设置静态变量:

To disable the default behaviour, you can tell PHPUnit to do so in your tests, e.g. within the setUp of your test or the test itself by setting a static variable in the global namespace:

# Warning:
PHPUnit_Framework_Error_Warning::$enabled = FALSE;

# notice, strict:
PHPUnit_Framework_Error_Notice::$enabled = FALSE;

另一种更改默认行为的选项是使用具有以下设置的XML文件:

Another option to change the default behaviour is to configure the testrunner with an XML file with the following settings:

<phpunit convertErrorsToExceptions="false"
         convertNoticesToExceptions="false"
         convertWarningsToExceptions="false">
</phpunit>

这三个选项不能作为命令行开关使用.

These three options are not available as command-line switches.

还请参见相关问题:测试会触发PHPUnit错误的方法的返回值.

这篇关于忽略PHPUnit中的PHP警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 22:17