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
-
11Seems to me that the unit test is working as it should. Fix the code so that it doesn't throw warnings or errors. – JJJ Dec 07 '11 at 09:11
-
@Juhana: I need only returns value of the function am not concerned with warnings.The function which am testing is working fine. – somu.web Dec 07 '11 at 09:19
-
2In a team working context you just sometimes have to test your own bundle, and you don't have time/credentials/mission to clean the others bundles (that may throw warnings just because they exist even not called) so the "Fix the code" comment seems unrelevant when you just want to test a specific function. – Xmanoux Jul 05 '16 at 12:11
-
Possible duplicate of [test the return value of a method that triggers an error with PHPUnit](http://stackoverflow.com/questions/1225776/test-the-return-value-of-a-method-that-triggers-an-error-with-phpunit) – Jeff Puckett Sep 16 '16 at 18:47
-
I'm 100% with you on this. I had a bunch of warnings appear and our GitHub workflows should have failed when they didn't. Instead of warning the test should have been allowed to fail/break. – Brad May 01 '22 at 23:19
3 Answers
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.
By default, PHPUnit converts PHP errors, warnings, and notices that are triggered during the execution of a test to an exception.
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).
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;
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.
See as well the related question: test the return value of a method that triggers an error with PHPUnit.
-
thank u it works with "# notice, strict: PHPUnit_Framework_Error_Notice::$enabled = FALSE; " – somu.web Dec 07 '11 at 09:54
-
3I tried to use this to test some file operations with vfsStream. Disabling PHP_Unit exceptions did not work (needed because of some chmod() which are not supported). I have to after all use good old silence operator '@'. – Zap Dec 22 '11 at 19:32
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:
<?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;
}
// ...
}
}

- 3,756
- 3
- 29
- 47
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:
composer require netsilik/base-test-case
Testing for an 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.

- 23,534
- 17
- 88
- 105