Writing Tests for D8

How it looks like

When I need it?

When? ONLY when I create something

  • good, great, mind-bending
  • reusable
  • extendable
  • supposed to live long life
  • vitally important
  • Before I write the code?
  • After?

 After all, do you really test all possibilities for error?

Of course NOT.

Benefits

  • you see when Functionality gets Broken
  • you know how to reproduce an issue
  • you can delegate fixing the issue without explaining it in details
  • you waste time in short terms to save it in long terms

"If I could save time in a bottle"

Types of Test within D8

  • Unit Tests
  • Kernel Tests
  • SimpleTests
  • JavascriptBaseTests
  • Behat, casperjs ... whatever

Types of Test within D8

  • Unit Tests
  • Kernel Tests
  • SimpleTests BrowserTestBase
  • JavascriptBaseTests
  • Behat, casperjs ... whatever

https://www.drupal.org/docs/8/phpunit

The testing framework PHPUnit was added to Drupal 8 in June of 2013. SimpleTest is still supported but is deprecated. Any new tests should be written using the PHPUnit based classes UnitTestCase, KernelTestBase, BrowserTestBase (web tests) or JavascriptTestBase (javascript enabled web tests using PhantomJS).

Where to start and What you need to know?

  • How to run test(s)
  • What types of tests there are? and when to choose one or another
  • How to debug tests

Running tests

  • setup environment
  • create reusable run script
  • run

https://www.drupal.org/docs/8/phpunit/running-phpunit-tests

#!/bin/bash

#start Solr
solr stop -all
solr start

# start the built-in php web server (mysql is already started)
#php -S localhost:8888 >& /dev/null &
#../vendor/bin/drush rs 8888 >& /dev/null &
php -S localhost:8888 ../vendor/drush/drush/commands/runserver/d8-rs-router.php >& /dev/null &

# Install the site.
../vendor/bin/drush -v site-install minimal --db-url=mysql://root@localhost/search_m1 --yes --account-pass=admin
../vendor/bin/drush en --yes simpletest

 # start phantomjs with gastonjs.
phantomjs --ssl-protocol=any --ignore-ssl-errors=true ../vendor/jcalderonzumba/gastonjs/src/Client/main.js 8510 1600 1080 > /tmp/pantomjs.out 2>&1 &
export SIMPLETEST_DB=mysql://root:qweszxc@localhost/search_m1
export SIMPLETEST_BASE_URL=http://localhost:8888

php core/scripts/run-tests.sh --verbose --color --non-html --concurrency 8 --php `which php` --url http://localhost:8888 --directory modules/contrib --types Simpletest | tee /tmp/tests_results.txt | grep passes; export TEST_EXIT=${PIPESTATUS[0]} ; echo $TEST_EXIT

# Check if we had fails in the run-tests.sh script
# Exit with the inverted value, because if there are no fails found, it will exit with 1 and for us that\
# is a good thing so invert it to 0. Travis has some issues with the exclamation mark in front so we have to fiddle a
# bit.
# Also make the grep case insensitive and fail on run-tests.sh regular fails as well on fatal errors.
TEST_OUTPUT=$(! egrep -i "([0-9]+ fails)|(Fatal erro)|([0-9]+ exceptions)" /tmp/test.txt > /dev/null)$?

../vendor/bin/phpunit -c core --group search_api,facets,search_api_solr,search_api_solr_multilingual --verbose | tee ; export TEST_PHPUNIT=${PIPESTATUS[0]} ; echo $TEST_PHPUNIT

# Exit the build
#echo $TEST_EXIT
echo $TEST_OUTPUT
echo $TEST_PHPUNIT

Running behat

Wanna try?

Help covering Conditional_Fields module with tests

https://code.adyax.com/Contrib/conditional_fields

What do I wanna know?

  • Reusable tests across projects
  • Your vision/suggestions for a must have test(s)
  • business expectations

Questions

T

By Ivan Tsekhmistro

T

  • 720