I am running continuous integration for a PHP project with Jenkins, Ant and PHPUnit 4.5.0. Jenkins's xUnit plugin will process the XML logs produced by PHPUnit.
Some significant errors (for example, referring to a file that is not pushed to VCS) only raise a PHP warning in PHPUnit, and warnings are not included in the log. Therefore the build is marked as successful even though it needs fixing.
How could I make the PHP warnings fail the build, for example by raising an exception for a test that produces warnings?
The divide(4,0) will then throw the expected exception and all the expect* function will pass. But note that the code $this->assertSame(0,1) will not be executed, the fact that it is a failure doesn't matter, because it will not run. The divide by zero exception causes the test method to exit.
In the PHP Explorer, right-click the file, and select New | PHPUnit Test Case. The New PHPUnit Test Case dialog is displayed. To select the element to be tested, click Browse next to the Tested Element field. The Element selection dialog is displayed.
PHPUnit is a unit testing framework for the PHP programming language. It is an instance of the xUnit architecture for unit testing frameworks that originated with SUnit and became popular with JUnit. PHPUnit was created by Sebastian Bergmann and its development is hosted on GitHub.
PHPUnit is a programmer-oriented testing framework for PHP. It is an instance of the xUnit architecture for unit testing frameworks. The currently supported versions are PHPUnit 9 and PHPUnit 8.
We have to add failOnWarning="true"
to treat such warnings as errors:
There was 1 warning:
1) The data provider specified for Tests\CreateSomethingTest::testCreateSomething is invalid.
FAILURES!
Tests: 841, Assertions: 2493, Failures: 1, Errors: 0.
So our config looks like:
<phpunit
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
stopOnFailure="false"
failOnWarning="true">
Make sure that options convert...ToExceptions
are set to true
. Unfortunately options are not available in command-line, so you have to create phpunit.xml
:
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
convertErrorsToExceptions = "true"
convertNoticesToExceptions = "true"
convertWarningsToExceptions = "true">
</phpunit>
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