Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

unit testing - Ignoring the PHP warnings in 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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...