How to Run Tests in PhpStorm

How to Run Tests in PhpStorm

PhpStorm offers so much functionality. From syntax highlighting to Docker integration, it’s an extremely comprehensive tool. However, have you ever thought of using it to run your unit tests? In this article, I step you through running tests, from an entire suite to an individual test.





Back on November 3rd, last year, I wrote a tutorial showing how to set up PHP debugging with PhpStorm, Xdebug, and PHPUnit. In this article, I’m taking that tutorial further by showing how to run tests in PhpStorm.

Now, you might wonder why you’d run tests in PhpStorm instead of from the command-line, that is if you’re a command-line junkie like myself. Well, while running them from the command-line is likely the best way — especially from an automation perspective — there are some time-saving features of running your PHP tests in PhpStorm.

To provide a little more context, while I was working on a small side-project recently, I came to appreciate the simplicity that PhpStorm offers, because I didn’t have to remember the command-line syntax for running either an individual test or all the tests in one class. Using PhpStorm can be an excellent way to “just get going”, quickly, simply, and efficiently.

Anyway, let’s get into each option. To run tests in you:

  • Pick the level at which you want to run tests (project, suite, directory, class, and test)
  • Choose one of “Run”, “Debug”, or “Run with Coverage”.

Now, let’s step through each of these.

Running all tests in a group in PhpStorm

For example, you can see in the image above, that I’ve right-clicked (or context-tapped) on a directory (Response) and have scrolled down to the test options. Depending on the option that I choose, it will run or debug all of the tests in that directory.

Running all tests in a class in PhpStorm

If I wanted to run all tests in a single class, I could either right-click on the class file in the project view (as in the image above) or right-click on the tab of an open file, and then choose the desired test option from the context menu that appears.

Running a single test in PhpStorm

Running individual tests is a little different, however. In the image above, you can see a right-facing, green arrow next to the test’s signature. Clicking on this opens a context menu specifically for testing, as in the image below. As with the previous approaches, you then choose the option which best suits your needs.

Running a single test in PhpStorm

Viewing Test Output

View test output in PhpStorm

Regardless of the option that you choose, however, when the tests are run, the Run panel opens (by default at the bottom of your PhpStorm window).

It is split into two panels. The panel on the left lists all of the tests that were run categorised based on how the tests were initiated. The panel on the right shows the test output.

In the image above, you can see some of the reasons why some of the tests failed. The image isn’t big enough to show all of the failed test output, but like most windows, you can scroll through the listed output. One advantage of PhpStorm’s Run panel is how it can compact output, making it easier to read and work with.

For example, if you have a test that asserts if two items were the same, such as two strings, it will hide the output, but let it be displayed in a popup window, as in the screenshot below.

The tests difference window in PhpStorm

Now here’s where it starts to get even more interesting — not to mention time-saving. Once you’ve run some tests for the first time, likely the majority will pass, but some will fail.

In the upper left-hand side of the Run panel, you can choose to re-run just the failed tests. That saves you from having to look through a large set of output if you have an extensive test suite.

Besides, you can (these are the buttons, from left to right, in the image above):

  • Show ignored tests
  • Sort the tests in alphabetical order (sometimes handy, sometimes not)
  • Sort tests by duration
  • Expand or collapse the test list
  • Step forward or back through the tests
  • View (and step through) the test history
  • Import tests from a file
  • Export test results

An especially handy option is to toggle auto-testing. This option automatically triggers tests to run when they’re updated. I don’t know of a way to do that on the command-line. I suspect it might be especially handy if I did know how to do it.

In Conclusion

And that’s an overview of how to run tests from within PhpStorm. PhpStorm offers more functionality, but what I’ve covered here will get you off to a great start.

I strongly encourage you to experiment and play around with the functionality on offer and find a workflow that works best for you. If you’ve been running tests in PhpStorm for a long time, what are your tips and suggestions? I’d love to know more about them in the comments.


You might also be interested in these tutorials too...

A Short Introduction to Postman
Fri, Jan 10, 2020

A Short Introduction to Postman

Debugging requests can be a time-consuming process. However, there’s a tool that makes doing so a lot simpler. It’s called Postman. In this tutorial, I step you through its core features and show you how to use them.

Go Interfaces Make Development and Testing Easier
Fri, Jul 21, 2023

Go Interfaces Make Development and Testing Easier

Substitutability or the Liskov Substitution Principle (LSP) is a concept that I’ve tried to adhere to for some years when writing code. It’s beneficial for many reasons, but particularly when testing, as it can indirectly force you to write code that is more testable. Recently, I’ve started appreciating how it works in Go, and will step through how in this short article.

Setup Step Debugging in PHP with Xdebug 3 and Docker Compose
Wed, Mar 10, 2021

Setup Step Debugging in PHP with Xdebug 3 and Docker Compose

In versions of Xdebug before version 3 setting up step debugging for code inside Docker containers has often been challenging to say the least. However, in version 3 it’s become almost trivial. In this short tutorial, I’ll step you through what you need to do, regardless of the (supported) text editor or IDE you’re using.


Want more tutorials like this?

If so, enter your email address in the field below and click subscribe.

You can unsubscribe at any time by clicking the link in the footer of the emails you'll receive. Here's my privacy policy, if you'd like to know more. I use Mailchimp to send emails. You can learn more about their privacy practices here.

Join the discussion

comments powered by Disqus