Skip to main content

Test driven development in PhpStorm with auto-testing enabled

Published on

When I work, I try to follow the principles of Test-Driven Development. I have found it to aid me in writing cleaner code, identifying odd coupling of components or crazy accidental dependencies between components. It also lets me write my API first by using mocks against interfaces I have defined. However, I am not going to talk about the what and whys about Test-Driven Development, I recommend picking up Kent Becks’ Test-Driven Development By Example for that. Also, Test-Driven Development will be referred to simply as TDD in the article.

One of the key aspects of TDD to is to write your test and assert expectations, and then write code. That means you will be running your tests — a lot. That means having to manually run your tests for each code change (as you should) will kill your velocity. That’s where PhpStorm’s auto-test functionality comes in. With auto-testing enabled you to keep development velocity while also executing your tests for each change. Auto testing has been around since PhpStorm 2017.2, so well over a year now. To be honest, I did not even know it was there until I got bored and hovered over the icon for the tooltip.

When you run your tests, it’s that icon with the green arrows going for a circular effect. For the life of me, I cannot find any other way to turn this on or find better words to describe this icon.

I always have a PhpStorm run configuration setup to execute my project’s entire PHPUnit tests. In a large project, there are hundreds of tests and the entire test suite can take 15 to 20 minutes. Having tests auto-run when working on a specific change set kind of kills the point of gaining development velocity if you are waiting that long to see if a change broke a test or not. If you wrote your tests properly, you should have a test class for each specific functionality, with different test methods for various scenarios. When working, target that specific class or scenario in a run configuration and use auto-run to hack away and run your tests.

Here’s a quick video of the process in action!

Did you like this? Would you be interested in more content about testing with test-driven development, behavior-driven development, performance-driven development? Or tools like PHPUnit or Behat? Or workflows I use in PhpStorm? Comment and let me know!

I'm available for one-on-one consulting calls – click here to book a meeting with me 🗓️