Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make PHPunit return nonzero exit status on warnings

Tags:

php

phpunit

When invoking PHPunit on some tests which fail with warnings, I get:

$ phpunit -c phpunit.xml --group app
Warning - MongoCollection::insert(): expects parameter 1 to be an array or object, null given in ...
    <more output>

OK, but incomplete or skipped tests!
Tests: 17, Assertions: 81, Incomplete: 1.

One of the tests should fail, but it doesn't; PHPunit marks it as "incomplete".

Let's check last exit status:

$ echo $?
0

The config I am using is:

<phpunit
    convertErrorsToExceptions="true"
    convertNoticesToExceptions="true"
    convertWarningsToExceptions="true"
    strict="true"
    stopOnError="true"
    stopOnFailure="true"
    stopOnIncomplete="true"
    stopOnSkipped="true"

    colors="true"
    bootstrap="bootstrap_phpunit.php"
    >

Any idea how to force PHPunit to emit nonzero exit status in case of "incomplete" tests?

like image 687
johndodo Avatar asked Nov 10 '22 06:11

johndodo


1 Answers

Thanks to gontrollez, I started looking into error handlers and finally found a solution:

set_error_handler(function ($severity, $message, $filepath, $line)
{
    throw new Exception($severity." - ".$message." - ".$filepath." - ".$line);
});

This code throws an exception which causes PHPunit to properly detect test as failed instead of incomplete. It should be put somewhere in bootstrap_phpunit.php (that is, in file, specified as bootstrap file).

like image 158
johndodo Avatar answered Dec 29 '22 02:12

johndodo