Skip to content

Instantly share code, notes, and snippets.

@jhedstrom
Created November 1, 2016 20:34
Show Gist options
  • Save jhedstrom/17a3d0ab48069c2a1bbd70533be91a31 to your computer and use it in GitHub Desktop.
Save jhedstrom/17a3d0ab48069c2a1bbd70533be91a31 to your computer and use it in GitHub Desktop.
PHPUnit based tests in Drupal 8

Local setup

  • Environment variables:

    export SIMPLETEST_DB=mysql://user:pass@localhost/db_name
    export SIMPLETEST_BASE_URL=http://d8.dev
    
  • or copy core/phpunit.xml.dist to core/phpunit.xml and edit accordingly

Running tests

 vendor/bin/phpunit -c ./core/phpunit.xml.dist --printer="\Drupal\Tests\Listeners\HtmlOutputPrinter" ./modules/custom/my_module
 vendor/bin/phpunit -c ./core/phpunit.xml --printer="\Drupal\Tests\Listeners\HtmlOutputPrinter" ./modules/custom/my_module/tests/src/Kernel/MyTestClass.php --filter=testFunctionName
  • The old core/scripts/run-tests.sh script still works, but not recommended

Writing tests

  • Unit
  • Kernel
  • Functional
  • Functional Javascript

Running Javascript tests

brew install phantomjs or follow instructions on phantomjs site

In separate terminal: phantomjs --ssl-protocol=any --ignore-ssl-errors=true PATH_TO_DRUPAL/vendor/jcalderonzumba/gastonjs/src/Client/main.js 8510 1024 768 2>&1 >> /dev/null &

Resources

Improvements

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment