Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make PHP warnings fail PHPUnit test cases?

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?

like image 528
borellini Avatar asked Feb 27 '15 14:02

borellini


People also ask

How do I assert exceptions in PHPUnit?

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.

How do I run a PHP test case?

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.

What is PHPUnit testing?

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.

Is PHPUnit a framework?

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.


2 Answers

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">
like image 96
Oleg Neumyvakin Avatar answered Nov 07 '22 10:11

Oleg Neumyvakin


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>
like image 31
Zdeněk Drahoš Avatar answered Nov 07 '22 10:11

Zdeněk Drahoš