1

I'm writing a contrib module but I can't find the documentation to run the tests - unit and functional. I know that I'm supposed to use PhpUnit, but I can't find examples of PhpUnit configuration files and more importantly, there is no resource on how to execute functional tests - i.e. having the module tested against a running Drupal instance.

I've been looking at some famous contrib modules like Paragraphs and there is nothing about running tests in their documentation - and no phpunit dependency at all. Which doesn't make sense to me since there are some functional tests implemented.

How are we supposed to provide tests for contrib modules?

0

You have to include phpunit/phpunit package in composer, alredy done if you took default drupal installation :

composer require phpunit/phpunit --dev

A file should be filled which describe yours testsuites, generally phpunit.xml

It looks like this ./phpunit.xml :

<phpunit bootstrap="web/core/tests/bootstrap.php" colors="true"
         beStrictAboutTestsThatDoNotTestAnything="true"
         beStrictAboutOutputDuringTests="true"
         beStrictAboutChangesToGlobalState="true"
         checkForUnintentionallyCoveredCode="false"
         printerClass="\Drupal\Tests\Listeners\HtmlOutputPrinter">

<php>
  <ini name="error_reporting" value="32767"/>
  <ini name="memory_limit" value="-1"/>
  <env name="SIMPLETEST_BASE_URL" value="http://drupal-8.localhost"/>
  <env name="SIMPLETEST_DB" value="mysql://drupal-8:drupal-8@localhost/drupal-8"/>
  <env name="BROWSERTEST_OUTPUT_DIRECTORY" value="/var/www/sites/default/simpletest"/>
</php>
  <testsuites>
    <testsuite name="testname1">
      <directory>web/modules/custom/your_module/tests</directory>
    </testsuite>
    <testsuite name="testname2">
      <directory>web/modules/custom/your_module2/tests</directory>
    </testsuite>
  </testsuites>
</phpunit>

In this example, drupal is located in ./web/ directory with ./vendor/, both in root path (./) .

You can add test for contrib module, but they are already tested (if they contain tests) when authors are pushing their sources to drupal.org ( https://www.drupal.org/pift-ci-job/1339317 ) :

<testsuite name="toolbar">
    <directory>./web/modules/contrib/admin_toolbar/tests/src/FunctionalJavascript</directory>
</testsuite>

Then you can run tests with :

./vendor/phpunit/phpunit/phpunit

To use a specific description file :

./vendor/phpunit/phpunit/phpunit -c phpunit-other.xml

To run a specific testsuite :

./vendor/phpunit/phpunit/phpunit --testsuite=testname1
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.