Skip to content

Go back

Fixing PHPUnit Issue: Empty lines in the output

Published:

Problem:

In a zero-config running of PHPUnit, you expect the output to be something like:

Terminal Screenshot

Where each dot represents a single test. Total of two tests in this example.

You get used to it. Until suddenly you notice something strange in the output, it’s coming like this:

Terminal Screenshot

Do you see what’s happening here?

THERE IS A FEW EMPTY LINES BETWEEN EACH DOT (test)

It messes up the output format in an extremely painful way.

It doesn’t matter if your configuration enables --colors or use another printer class like PHPUnit\Util\TestDox\CliTestDoxPrinter (which can be used by the --testdox flag).

Terminal Screenshot

These few empty lines are still there. And it’s hard to ignore it.


Solution:

Turns out, it’s just because one of your PHP files contains these empty lines either before the PHP opening tag <? or after the closing tag ?>.

That’s it.

It’s not a PHPUnit bug.

Thus, The title “Fixing PHPUnit issue” is not accurate. However, there is a higher chance that you will face this issue on the PHPUnit console output. For example this post on Coderwall.


Explanation

When each test starts, PHPUnit will autoload the classes provided by Composer’s autoload map.

Composer’s autoloading feature, in the end, it’s a better and manageable way of handling multiple require "xxxxx.php"; statements.

The require statement in PHP (also include) when used to load a file with some input before the PHP opening tag <? or after the closing tag ?>, it will send this input to the buffer output.

Terminal Screenshot

PHPUnit will handle the buffer output (using ob_start and ob_get_contents functions) generated by the test itself, and print it out after each test.

That’s being said, this can happen to any input. It’s not limited to “empty extra lines”. But, as PHP developers, there is a higher chance of us mistakenly leaving empty lines than leaving some random input on .php files.

Comments Section

Note from the author:

I'm really sorry I'm using Disqus as a commenting platform. One day I'll change it to a better solution.