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
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.
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; } // ... } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With