Skip to end of metadata
Go to start of metadata

Redirection Notice



This tutorial describes how to create PHPUnit Tests (test classes and methods) right from the PhpStorm IDE.

Starting with PhpStorm 6.0, PHPUnit Skeleton Generator is no longer available in the IDE. PHPUnit tests creation functionality has been completely re-worked so that PhpStorm generates tests itself and you can benefit from its flexibility and code insight. The IDE already knows a lot about your code and your project and uses this information for effective code generation.

1. Test Classes Generation

In general, Test Class is a normal PHP class that conforms with specific rules such as:

  • A special name (e.g. tests for a class Class should go into a class ClassTest) 
  • Test Class inherits from PHPUnit_Framework_TestCase

In most cases the IDE can help us create a Test Class with a set of actions.

The easiest way to create a test is to invoke the action "New ..." (Context menu on folder/file in the Project View, or File | New, or Cmd-N / Alt+Insert keystroke) and select New | PHPUnit | PHPUnit Test. When invoked on a specific file that contains Class, there is also New | PHPUnit | ClassTest (where Class is the name of class in the file) - Class name is automatically filled in the dialog Fully qualified name field for convenience:

Let's examine the Create New PHPUnit Test dialog in details:

  • If the test is for some specific class, the class can be chosen with autocompletion in the Fully Qualified Name field. Class name will be also used to propose Test Class Name.
  • Autocompletion can be also used in Namespace and Directory fields.
  • By default, the Directory field is auto-completed to conform with directory where New Test Class action has been invoked.
  • Namespace field is auto-completed by default taking into consideration the Directory.
  • When the Directory is changed, the Namespace is changed accordingly.

In order to create a test for some specific class, we can invoke Create new test after Go to Test action with Navigate | Go to Test or Cmd-Shift-T / Ctrl-Shift-T. The class name will be used for the Create New PHPUnit Test dialog.

When the test is ready, we can navigate back to the tested class by invoking Navigate to Test Subject action in the same way: with Cmd-Shift-T / Ctrl-Shift-T or Navigate | Go to Test Subject.

2. Test Methods Generation

To create a test method for a test class, we can invoke Generate | PHPUnit Test Method (Context Menu | Generate, or Code | Generate, or Cmd-N / Alt+Insert keystroke) inside the test class.

To generate Set Up and Tear Down, we can use the Generate | Override method action.

  • No labels