Testing improvements in PhpStorm 2018.1

PhpStorm 2018.1 brings several improvements to streamline and simplify your work with PHPUnit tests. You can now create target classes from tests, annotate your tests easily, and run the tests using the code editor gutter icons. Let’s take a closer look at the new features.

Creating a class and class methods from a test

In PhpStorm, creating a test from a class is as simple as pressing Ctrl+Shift+T (Cmd+Shift+T on macOS). With TDD becoming more and more popular, it may happen that the test is already in place, but the class being tested does not exist yet.

To handle such situations, PhpStorm 2018.1 introduces the Missing target element for PHPUnit test inspection and the corresponding quick-fix. The inspection will detect if a class or a method being tested is not yet created. To fix this, simply position the caret on an element, invoke the quick-fix by pressing Alt-Enter, and execute the required command:

create_class_from_test

PhpStorm will generate the missing elements in their respective locations, taking your PSR-0/PSR-4 structure into account.

As with other inspections, the Missing target element for PHPUnit test inspection can be used for applying changes in bulk. Simply invoke Run inspection by name and select the inspection:

create_methods_from_test

Improved @covers support

In PhpStorm 2018.1, we’ve significantly enhanced the support for the PHPUnit @covers and @coversDefaultClass annotations.

As soon as you provide the @coversDefaultClass annotation for a test class, name completion will become available:

covers_default_class

With the covered class in place, PhpStorm will pick out the relevant class methods to annotate with @covers and suggest auto-completion for them. The new inspection will also detect all usages of @covers with methods and functions not prepended with :: (the scope resolution operator).

covers_default_class_method

By invoking code completion (CMD/CTRL+Space) twice, you can also complete visibility modifiers, which will allow you to specify what the annotated test method covers, for example, all the non-public methods of a class.

Navigating to tests

Since both the target methods and the @covers annotations are in place, it has become possible to navigate from a method to all test methods that cover it. Simply position the caret on a method and invoke the Navigate to Test action. This way, you can create new tests, too:

navigate_to_test

Test method names auto-completion

PhpStorm 2018.1 also provides automatic completion of test method names. If, say, methodC has been added to the above example class, it will be detected from within the test class. As a result, you can create the corresponding test method stub with a conventional name in a matter of seconds:

create_test_method

Gutter icons for running tests

In earlier PhpStorm versions, running tests from the code editor was only possible with the help of the excellent PHPUnit Enhancement plugin. In PhpStorm 2018.1, this functionality is implemented natively. Using the gutter icons, you can invoke the Run, Debug, and Run with Coverage actions for PHPUnit tests:

gutter_icons

Gutter icons work both for Local and Remote Interpreters, and reflect the previous tests execution state: passed, failed, or not started.

Stay tuned for more updates!

Your JetBrains PhpStorm Team
The Drive to Develop

This entry was posted in Cool Feature, Newsletter and tagged , , . Bookmark the permalink.