Streamlining PHPUnit Test Output in VS Code

Janez Cergolj • May 31, 2023

For a long time, I found PHPUnit's output in the terminal to be cumbersome. When multiple tests failed, it became challenging to locate the actual failed assertion among the stack of errors. This inspired me to find a solution for my VS Code editor. In this blog post, I will share my approach and workflow to improve the PHPUnit test output experience.

phpunit failed test example Does this looks familiar? The image above illustrates an example of a failed PHPUnit test. As you can see, finding the relevant failed assertion requires scrolling through the terminal output, which can be time-consuming.

To enhance my workflow, I devised a simple solution. Here's how it works:

PHPUnit BASIC Workflow

Create a file called .phpunit-output.txt.
Add .phpunit-output.txt to the .gitignore file.
Run the command phpunit > .phpunit-output.txt in the terminal. Result is saved to the file.

This basic workflow ensures that the PHPUnit test results are saved in the .phpunit-output.txt file, improving accessibility and reducing the need for terminal scrolling.

Improved Workflow for VS Code Editor

To further enhance the workflow within the VS Code editor, follow these steps:

Create a file called .phpunit-output.txt.
Add .phpunit-output.txt to the .gitignore file.
Run the command code -r .phpunit-output.txt && phpunit > .phpunit-output.txt in the terminal. Opens file and save result to the file.

By using this improved workflow, the .phpunit-output.txt file opens automatically in VS Code, allowing you to easily view and navigate through the PHPUnit test results.

Bonus: Create an Alias

To simplify the process, consider adding an alias to your ~/.bashrc file:

alias pof='code -r .phpunit-output.txt && phpunit > .phpunit-output.txt'

Don't forget to run source ~/.bashrc to apply the changes.


With this simple hack, my testing workflow has become much more streamlined. After PHPUnit finishes, I navigate to the phpunit-output.txt file and fix the tests one by one using the command phpunit --filter=FailedTest. Once a test is fixed, I remove its failed assertion from the phpunit-output.txt file.

By adopting this approach, you can improve the readability and efficiency of your PHPUnit test output, making it easier to pinpoint and resolve test failures.