Code Studio

Adding a PHPUnit test to the Code Studio pipeline

Prerequisites

Before adding a PHPUnit test to your Code Studio pipeline, you must:

  1. Understand how to implement PHPUnit tests.

  2. Include drupal/core-dev and phpunit/phpunit in the composer.json file of your project.

  3. Place the phpunit.xml.dist file in the root directory of your project.

  4. Ensure that a custom module exists in your codebase.

Adding a PHPUnit test to your custom module

  1. In your custom module, create a directory for tests/src/Functional/.

    For example, docroot/modules/custom/my_module/tests/src/Functional.

  2. Add a PHPUnit test file.

    For example, ExampleTest.php.

  3. Add your PHPUnit test cases to the test file.

Updating the PHPUnit configuration file

In the phpunit.xml.dist file, ensure that the path to your custom modules directory exists under a test suite. For example, if your module exists in the docroot/modules/custom/ directory, ensure that the phpunit.xml.dist file contains the following:

<?xml version="1.0"?>
<!-- phpunit.xml.dist -->
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
   <coverage processUncoveredFiles="true">
      <include>
         <directory suffix=".php">src</directory>
      </include>
      <report>
         <clover outputFile="build/logs/clover.xml"/>
      </report>
   </coverage>
   <testsuites>
      <testsuite name="Custom modules">
         <directory>docroot/modules/custom/</directory>
      </testsuite>
   </testsuites>
   <logging/>
</phpunit>

Adding project variables

  • The following variables must be set in your project:

    • ACQUIA_TASKS_SETUP_DRUPAL must be set to true. For more information, see Adding environment variables.

    • If ACQUIA_TASKS_SETUP_DRUPAL_CONFIG_IMPORT exists, it must be set to true.

    • If ACQUIA_TASKS_SETUP_DRUPAL_STRATEGY exists, it must be set to install.

    • If ACQUIA_TASKS_PHPUNIT exists, it must be set to true.

    • If ACQUIA_JOBS_TEST_DRUPAL exists, it must be set to true.