provided as part of core • Drupal 8 - PHPUnit added as a core dependency, later became the default via the PHPUnit initiative • Drupal 9 - SimpleTest removed from core, moved back to contrib @opdavies
extension • tests/src directory within each module • Within the Drupal\Tests\{module_name} namespace • Class name must match the filename • Namespace must match the directory structure • One test class per feature @opdavies
API • Calculating attendance figures for an event • Determining if an event is purchasable • Promotions and coupons for new users • Cloning events • Queuing private message requests • Re-opening closed support tickets when comments are added @opdavies
Test run started: Saturday, October 14, 2023 - 10:28 Test summary ------------ Drupal\Tests\example\Functional\ExamplePageTest 1 passes Test run duration: 7 sec @opdavies
run some types of tests • Ignored by Git by default • Copy core/phpunit.xml.dist to core/phpunit.xml • Add and change as needed • SIMPLETEST_BASE_URL, SIMPLETEST_DB, BROWSERTEST_OUTPUT_DIRECTORY • stopOnFailure="true" @opdavies
in Drupal. • Application URL links users to separate application system. • Constructed from domain, includes role ID as a GET parameter and optionally UTM parameters. • Jobs need to be linked to offices. • Job length specified in number of days. • Path is specified as a field in the API. @opdavies
XML • Added system user with API role to authenticate • active_for converted from number of days to UNIX timestamp • branch_name and locations converted from plain text to entity reference (job node to office node) • url_alias property mapped to path @opdavies
the correct URL and the correct response code is returned • FunctionalJavaScript: application URL is updated with JavaScript based on UTM parameters (hosting) • Kernel: job nodes can be added and deleted, expired job nodes are deleted, application URL is generated correctly • Unit: ensure number of days are converted to timestamps correctly @opdavies
functional tests • Drop down to integration or unit tests where needed • Programming by wishful thinking • Write comments first, then fill in the code • Sometimes write assertions first @opdavies
57 tests, 180 assertions • Kernel - 38 tests, 495 assertions • Unit - 5 tests, 18 assertions Run in 2-3 minutes in a CI pipeline with GitHub Actions. @opdavies
There was 1 error: 1) Drupal\Tests\drupalcon\Functional\BlogPageTest::it_loads_the_blog_page Behat\Mink\Exception\ExpectationException: Current response status code is 404, but 200 expected. /app/vendor/behat/mink/src/WebAssert.php:794 /app/vendor/behat/mink/src/WebAssert.php:130 /app/web/modules/custom/drupalcon/tests/src/BlogPageTest.php:16 /app/vendor/phpunit/phpunit/src/Framework/TestResult.php:728 ERRORS! Tests: 1, Assertions: 2, Errors: 1. @opdavies
There was 1 error: 1) Drupal\Tests\drupalcon\Functional\BlogPageTest::it_loads_the_blog_page Behat\Mink\Exception\ExpectationException: Current response status code is 404, but 200 expected. /app/vendor/behat/mink/src/WebAssert.php:794 /app/vendor/behat/mink/src/WebAssert.php:130 /app/web/modules/custom/drupalcon/tests/src/BlogPageTest.php:16 /app/vendor/phpunit/phpunit/src/Framework/TestResult.php:728 ERRORS! Tests: 1, Assertions: 2, Errors: 1. @opdavies
There was 1 error: 1) Drupal\Tests\drupalcon\Functional\BlogPageTest::it_loads_the_blog_page Behat\Mink\Exception\ExpectationException: Current response status code is 404, but 200 expected. /app/vendor/behat/mink/src/WebAssert.php:794 /app/vendor/behat/mink/src/WebAssert.php:130 /app/web/modules/custom/drupalcon/tests/src/BlogPageTest.php:16 /app/vendor/phpunit/phpunit/src/Framework/TestResult.php:728 ERRORS! Tests: 1, Assertions: 2, Errors: 1. @opdavies
There was 1 error: 1) Drupal\Tests\drupalcon\Functional\BlogPageTest::it_loads_the_blog_page Behat\Mink\Exception\ExpectationException: Current response status code is 404, but 200 expected. /app/vendor/behat/mink/src/WebAssert.php:794 /app/vendor/behat/mink/src/WebAssert.php:130 /app/web/modules/custom/drupalcon/tests/src/BlogPageTest.php:16 /app/vendor/phpunit/phpunit/src/Framework/TestResult.php:728 ERRORS! Tests: 1, Assertions: 2, Errors: 1. @opdavies
There was 1 error: 1) Drupal\Tests\drupalcon\Functional\BlogPageTest::it_loads_the_blog_page Behat\Mink\Exception\ExpectationException: Current response status code is 404, but 200 expected. /app/vendor/behat/mink/src/WebAssert.php:794 /app/vendor/behat/mink/src/WebAssert.php:130 /app/web/modules/custom/drupalcon/tests/src/BlogPageTest.php:16 /app/vendor/phpunit/phpunit/src/Framework/TestResult.php:728 ERRORS! Tests: 1, Assertions: 2, Errors: 1. @opdavies
(100%) Time: 00:01.532, Memory: 6.00 MB There was 1 error: 1) Drupal\Tests\drupalcon\Functional\BlogPageTest::it_loads_the_blog_page Behat\Mink\Exception\ExpectationException: Current response status code is 403, but 200 expected. @opdavies
1 (100%) Time: 00:01.906, Memory: 6.00 MB There was 1 error: 1) Drupal\Tests\drupalcon\Functional\BlogPageTest::it_loads_the_blog_page Behat\Mink\Exception\ExpectationException: Current response status code is 500, but 200 expected. @opdavies
There was 1 error: 1) Drupal\Tests\drupalcon\Kernel\ArticleRepositoryTest::it_returns_blog_posts Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException: You have requested a non-existent service "Drupal\Tests\drupalcon\Kernel\ArticleRepository". E 1 / 1 (100%) Time: 00:00.405, Memory: 6.00 MB There was 1 error: @opdavies
There was 1 failure: 1) Drupal\Tests\drupalcon\Kernel\ArticleRepositoryTest::it_returns_blog_posts Failed asserting that actual size 0 matches expected size 1. @opdavies
There was 1 error: 1) Drupal\Tests\drupalcon\Kernel\ArticleRepositoryTest::it_returns_blog_posts Drupal\Component\Plugin\Exception\PluginNotFoundException: The "node" entity type does not exist. 1 public static $modules = [ 2 'drupalcon', 3 'node', 4 ]; 5 @opdavies
There was 1 failure: 1) Drupal\Tests\drupalcon\Kernel\ArticleRepositoryTest::it_returns_blog_posts Failed asserting that actual size 0 matches expected size 1. @opdavies
There was 1 failure: 1) Drupal\Tests\drupalcon\Kernel\ArticleRepositoryTest:: only_published_articles_are_returned Failed asserting that actual size 5 matches expected size 3. @opdavies