Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

PhpStorm uses PHPUnit as the test runner, a widely used unit testing framework for PHP which provides a lot of features. Let's see how it integrates with PhpStorm.

Table of Contents

1. Adding a unit test to a project

Adding unit tests to a project can be done in several ways. We can create a new file and select the PHPUnit | PHPUnit test file template. Another way to create a test for some specific class is to invoke use the Go to Test action (with Navigate | Go to Test or Cmd+Shift+T / Ctrl+Shift+T) and choose Create new test.

...

Tip

For more info on creating unit tests, see creating PHPUnit Tests in PhpStorm.

2. Enabling PHPUnit for our project

As we can see from the generated class above, there is no autocompletion support yet and PhpStorm does not know about the PHPUnit_Framework_TestCase class PHPUnit provides. Why is that? Because we haven't enabled PHPUnit yet for our project. Let's do that.

...

Note

If you want to have an example class to test as well as a sample set of unit tests for it, checkout our Workshop project.

3. Running unit tests

The easiest way to run PHPUnit test in PhpStorm is to right click a test and select > Run 'test name'. A test class/separate method can be run by right clicking it directly in editor or by selecting a class file in a Project Tool window. Once run, PHPUnit run configuration will be automatically created: you can view it in Run menu > Edit configurations... . It should appear under PHPUnit node and named after a test file we've run.

...

We can also manually create a test run configuration but that it is often too cumbersome and should be reserved for the advanced PHPUnit test configurations. When we create a new PHPUnit run configuration (or edit existing one) we can provide details such as run configuration name, which tests should be run (all tests in a directory, a specific test class or one specific test). Adding test runner arguments or PHP interpreter options is also supported. 

 

4. Reviewing test results

We are now ready to run (or debug) our unit tests! Let's do this: just right click a PHPUnit test and select "Run 'test name' ...". This will bring up a new tool window in which test results will be displayed.

...