忽略 PHPUnit 中的 PHP 警告

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8412746/
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-26 04:37:46  来源:igfitidea点击:

Ignoring the PHP warnings in PHPUnit

phpunit-testingphpunit

提问by somu.web

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

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

回答by hakre

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.

正如 Juhana 评论的那样,您应该首先修复出现警告的代码。这是代码工作不正常/严格的标志。

By default, PHPUnit converts PHP errors, warnings, and notices that are triggered during the execution of a test to an exception.

默认情况下,PHPUnit 将测试执行期间触发的 PHP 错误、警告和通知转换为异常。

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

请参阅测试 PHP 错误,其中有更多信息如何测试您的警告(以及如何忽略您在测试中调用的子例程中的警告)。

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

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

# Warning:
PHPUnit_Framework_Error_Warning::$enabled = FALSE;

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

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

更改默认行为的另一个选项是使用具有以下设置的 XML 文件配置测试运行程序:

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

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

这三个选项不能用作命令行开关。

See as well the related question: test the return value of a method that triggers an error with PHPUnit.

另请参阅相关问题:测试触发 PHPUnit 错误的方法的返回值

回答by Courtney Miles

The documentented strategy to do this at a per-test level is to use the @error suppression operator when your test calls the function that would trigger a warning or notice.

在每个测试级别执行此操作的已记录策略是@在您的测试调用会触发警告或通知的函数时使用错误抑制运算符。

The following code is the example from the PHPUnit documentation:

以下代码是PHPUnit 文档中的示例:

<?php
class ErrorSuppressionTest extends PHPUnit_Framework_TestCase
{
    public function testFileWriting() {
        $writer = new FileWriter;
        $this->assertFalse(@$writer->write('/is-not-writeable/file', 'stuff'));
    }
}
class FileWriter
{
    public function write($file, $content) {
        $file = fopen($file, 'w');
        if($file == false) {
            return false;
        }
        // ...
    }
}

回答by Jacco

You should not ignore warnings, they are there for a reason. Having said that, warnings and notices are not intended to be fatal (they would have been an error if they were intended to be fatal).

您不应该忽略警告,它们存在是有原因的。话虽如此,警告和通知并不是要致命的(如果它们是致命的,它们就会是一个错误)。

Instead of ignoring the warning, you should test for it in a unit test. You can do so by using Netsilik/BaseTestCase(MIT License). With this extension to PHPUnit, you can test directly for triggered Errors/Warnings, without converting them to Exceptions:

您应该在单元测试中对其进行测试,而不是忽略警告。您可以通过使用Netsilik/BaseTestCase(MIT 许可证)来实现。使用 PHPUnit 的这个扩展,您可以直接测试触发的错误/警告,而无需将它们转换为异常:

composer require netsilik/base-test-case

composer require netsilik/base-test-case


Testing for an E_USER_NOTICE:


测试一个E_USER_NOTICE

<?php
namespace Tests;

class MyTestCase extends \Netsilik\Testing\BaseTestCase
{
    /**
     * {@inheritDoc}
     */
    public function __construct($name = null, array $data = [], $dataName = '')
    {
        parent::__construct($name, $data, $dataName);

        $this->_convertNoticesToExceptions  = false;
        $this->_convertWarningsToExceptions = false;
        $this->_convertErrorsToExceptions   = true;
    }

    public function test_whenNoticeTriggered_weCanTestForIt()
    {
        $foo = new Foo();
        $foo->bar();

        self::assertErrorTriggered(E_USER_NOTICE, 'The warning string');
    }
}

Hope this helps someone in the future.

希望这对将来的人有所帮助。