PhpStorm 2018.1 Help

Testing with Codeception

PhpStorm provides support for running unit, functional, and acceptance tests with the Codeception test framework, versions 2.2.0 and later.

Before you start

Make sure the PHP interpreter is configured in PhpStorm on the PHP page, as described in Configuring Local PHP Interpreters and Configuring Remote PHP Interpreters.

Where do I get Codeception from?

Option 1: Download codeception.phar

  1. Download codeception.phar at the Codeception Installation page.
  2. Save codeception.phar under the root of the project where Codeception will be later used. You can also save codeception.phar anywhere else and configure it as an include path. In either case, PhpStorm will include codeception.phar in indexing, so PhpStorm will successfully resolve references to Codeception classes and thus provide you with full coding assistance.

Option 2: Use Composer

  1. On the context menu of composer.json, choose Composer | Manage Dependencies.
  2. In the Add Composer Dependency dialog that opens, select the codeception/codeception package from the Available Packages list, possibly using the search field. The list shows all the available packages, the packages that are already installed are marked with a tick.

    Choose the relevant version from the Version to install list.

  3. If necessary, expand the Settings hidden area and specify the advanced installation options. In the Command line parameters text box, type the additional command line parameters. For example, to have the package added to the require-dev section of the composer.json file instead of the default require section, type --dev. For more information about using the Composer command line options during installation, refer to the Composer documentation.
  4. Click Install.
Learn more about installing Codeception from Codeception Official website.

How do I initialize Codeception in a project?

To generate a codeception.yml configuration file, open the built-in PhpStorm Terminal (Alt+F12) and at the command prompt type one of the following commands depending on the installation mode and your current operating system:

  • If you installed codeception.phar in your project, type php codecept.phar bootstrap for Windows and macOS or codecept bootstrap for Linux.
  • If you installed Codeception through Composer, type codecept bootstrap for all platforms.

How do I integrate Codeception with PhpStorm in a project?

Step 1: Choose how to use Codeception

Open the Settings / Preferences dialog by pressing Ctrl+Alt+S, or alternatively choose File | Settings on Windows and Linux or PhpStorm | Preferences on macOS. Expand the Languages and Frameworks node and select Test Frameworks under PHP.

On the Test Frameworks page that opens, click add.png in the central pane and choose the configuration type from the list:

ps_settings_php_test_frameworks.png

Step 2: Choose the PHP interpreter to use
To use Codeception with a remote PHP interpreter, choose one of the configurations from the dialog box that opens:

ps_settings_php_test_frameworks_codeception_choose_php_interpreter.png

Step 3: Specify the Codeception library to use
In the Codeception Library area, specify the location of the Codeception executable file or codeception.phar archive in the target environment. For example, if you installed Codeception through Composer, the executable file is stored in vendor/bin/codecept. Click refresh next to the Path to Codeception directory or phar file text box. PhpStorm detects the version of Codeception and displays it below the text box.

Step 4: Specify the Codeception configuration file to use
In the Test Runner area, appoint the configuration .yml file to use for launching and executing scenarios.

By default, Codeception looks for a codeception.yml configuration file in the project root folder. You can appoint a custom configuration file.

  • Clear the Default configuration file checkbox to have Codeception use the codeception.yml configuration file from the project root folder. If no such file is found, test execution fails, therefore it may be more reliable to specify the configuration file explicitly.
  • Select the Default configuration file checkbox to specify your own .yml configuration file. This file will be later used as default in all Codeception run/debug configurations.

    In the text box, specify the location of the configuration file to use. Type the path manually or click browseButton and choose the file in the dialog box that opens.

How do I generate a Codeception test for a class?

Step 1: Open the Create New PHP Test dialog

Do any of the following:

  • On the main menu, choose File | New. Then, choose either PHP Test | Codeception Unit Test or PHP Test | Codeception Functional Test from the pop-up menu.
  • In the Project tool window, press Alt+Insert or right-click the PHP class to be tested and choose either New | PHP Test | Codeception Unit Test or New| PHP Test | Codeception Functional Test.
  • In the editor of the PHP class to be tested, position the caret inside the definition of the class. Then, choose Go To | Test on the context menu or press Ctrl+Shift+T and select Create New Test from the pop-up menu. This way, you can generate a test for a PHP class defined among several classes within a single PHP file.

The Create New PHP Test dialog opens:

ps create new php test codeception dialog

Step 2: Configure test generation

In the Create New PHP Test dialog, specify the following:

  1. The test file template, that is, the template based on which PhpStorm will generate the test class. Make sure that either Codeception Unit or Codeception Functional is selected in the Test file template list.
  2. The fully qualified name of the class to be tested, this name will be used to propose the Test Class Name. To use completion, press Ctrl+Space.
  3. The name of the test class. PhpStorm automatically composes the name from the production class name as <production class>Cest.php (for Codeception functional tests) or <production class>Test.php (for Codeception unit tests). The test class name is displayed in the Name text box of the Test Class area.
  4. The folder for the test class. By default, it is the folder where the production class is stored. To specify a different folder, click browseButton next to the Directory text box and choose the relevant folder.
  5. The production class methods to generate test method stubs for. In the Generate test methods for area, select the checkboxes next to the required production class methods. PhpStorm will automatically compose the test methods' names as test<production method>. You can customize the code templates used for generating test method stubs on the Code tab of the File and Code Templates settings page.
  6. When the test is ready, navigate back to the production class by choosing Navigate | Go to Test Subject. For details, see Navigating Between Test and Test Subject.

Step 3: Launch test generation
Check, accept, or update the predefined settings and click OK to initiate the test generation.

How do I run and debug Codeception tests?

For information about writing Codeception tests, see Unit Tests, Acceptance Tests, and Functional Tests. To run or debug your tests, do one of the following:

Option 1: To run or debug Codeception tests
In the Project tool window, select the file or folder to run your tests from and choose Run <file_or_folder_name> or Debug <file_or_folder_name> on the context menu of the selection:

ps_test_frameworks_codeception_launch_tests.png
PhpStorm generates a default run configuration and starts a run/debug test session with it.

Option 2: To save an automatically generated default configuration
After a test session is over, choose Save <default_test_configuration_name> on the context menu of the file or folder and choose Save <default_configuration_name> on the context menu.

Option 3: To run or debug tests through a previously saved run/debug configuration
Choose the required Codeception configuration from the list on the tool bar and click run or debug.

Option 4: To create a custom run/debug configuration

  1. In the Project view, select the file or folder with the tests to run and choose Create run configuration on the context menu. Alternatively, choose Run | Edit Configurations on the main menu, then click add and choose Codeception from the list.
  2. In the Run/Debug Configuration: Codeception dialog that opens, specify the tests to run and customize the behavior of the current PHP interpreter by specifying the options and arguments to be passed to the PHP executable file.

How do I monitor test results?

PhpStorm shows the results of test execution in the Test Runner tab of the Run Tool Window. The tab is divided in 2 main areas. In the left-hand area you can drill down through all unit tests to see the succeeded and failed ones. In this area you can also filter tests and export results.
The right-hand area displays the raw Codeception output:

ps_test_result_codeception.png
Use the context menu in the left-hand area to run specific tests or navigate to the source code.

Last modified: 9 April 2018

See Also