I am a very big fan of test-driven development (TDD) and I believe in using tests to verify that a codebase is reliable and stable. I believe in using TDD to produce stable systems
and to effectively test software and ensure that my design actually adheres to the problem that I was trying to solve. I tend to run tests often as it then makes it very clear
if a change in the system has broken something and I know immediately that it was something which was recently added. Robert "Uncle Bob" Martin summarised this testing methodology in a very elegant
"Every hour you are producing several tests. Every day dozens of tests. Every month hundreds of tests. Over the course of a year you will write thousands of tests. You can keep all these tests and run them any time you like! When would you run them? All the time! Any time you made any kind of change at all!"
If you adopt this approach then you often have to run your test suite (or specific tests) quite often and doing so in a quick and efficient manner as part of your workflow will save you time in the long run.
I use PHPStorm for most of my dev and PHPStorm has an awesome Test Suite feature which you can use to run your tests from your IDE. We will cover how to set this up with PHPUnit for your own
Setting up PHPStorm Test Suite
In our example, we are going to look at PHPUnit as this is a testing framework that I use often however the testing suite feature currently
has support for PHPSpec, Codeception and Behat by default.
Navigate to Test Frameworks Option
To start, go to your settings menu and then select Languages & Frameworks > PHP > Test Frameworks
Once there, click the add (+) button and select PHPUnit local (assuming you are running your project on your local machine).
PHPUnit Test Suite Configuration
My app uses Composer to pull in all dependencies including PHPUnit so select "use Composer autoloader".
Select the path to your PHPUnit binary, in this case it's in my vendor folder.
You can optionally set a default configuration file which in this case I have set to phpunit.xml as that is where my PHPUnit settings are defined for this test suite. One
can also set the path to a bootstrap file if it's not defined in your configuration file or you can leave these options off.
That's it, short and sweet. You have now configured PHPUnit as a test framework in PHPStorm.
Setting up Run Context Configuration Keybindings
A very efficient way of running your tests is just to use a keybinding that allows you to run your test suite
at a moment's notice.
In order to set this up. Go to your settings menu and then setup two keybindings.
- Run Context Configuration - This allows you to run your entire test suite at a moment's notice. I currently have this setup as Option+T
- Run with Coverage Context Configuration - This will run the current test file that you are writing. Very useful when trying to write the first step of your tests namely the failing tests. I use Option+C
That's all there is to it! PHPStorm really makes the setup of Test Frameworks very simple. You can now use the keybindings to run your test
framework at a moment's notice. You should see the output of your tests in the Test Runner tab.
Oops, look like I made a mistake. Good thing I run my tests often :)
Let me know if you have any questions or feedback in the comments below.