<p>Most of my work uses the Drupal framework, and I've given talks and workshops on automated testing and building custom Drupal modules with test-driven development. Today, I wanted to see how quickly I could get a working test suite on a new Drupal project.</p>
<p>I cloned a fresh version of my <a href="https://github.com/opdavies/docker-examples">Docker Examples repository</a> and started the Drupal example.</p>
<p>I ran <code>mkdir -p web/modules/custom/example/tests/src/Functional</code> to create the directory structure that I needed, and then <code>touch web/modules/custom/example/tests/src/Functional/ExampleTest.php</code> to create a new test file and populated it with some initial code:</p>
<pre><code class="language-php"><?php
namespace Drupal\Tests\example\Functional;
use Drupal\Tests\BrowserTestBase;
use Symfony\Component\HttpFoundation\Response;
class ExampleTest extends BrowserTestBase {
protected $defaultTheme = 'stark';
}
</code></pre>
<p>For the simplest test, I decided to test some existing Drupal core functionality - that an anonymous user can view the front page:</p>
<pre><code class="language-php">/** @test */
public function the_front_page_loads_for_anonymous_users() {
<p>To execute the test, I ran <code>SIMPLETEST_DB=sqlite://localhost//dev/shm/test.sqlite SIMPLETEST_BASE_URL=http://web phpunit -c web/core web/modules/custom</code>. The environment variables could be added to a <code>phpunit.xml.dist</code> file but I decided to add them to the command and use Drupal core's PHPUnit configuration file.</p>
<p>As this is existing functionalty, the test passes. I can change either the path or the response code to ensure it also fails when expected.</p>
<p>With the first test working, it's easy to add more for other functionality, such as whether different users should be able to access administration pages:</p>
<pre><code class="language-php">/** @test */
public function the_admin_page_is_not_accessible_to_anonymous_users() {
<p>Hopefully, this shows how quickly you can get tests running for a Drupal module. If you'd like to see more, the slides and video recording of my <a href="/presentations/tdd-test-driven-drupal">Test-Driven Drupal talk</a> are online.</p>
format:full_html
processed:|
<p>Most of my work uses the Drupal framework, and I've given talks and workshops on automated testing and building custom Drupal modules with test-driven development. Today, I wanted to see how quickly I could get a working test suite on a new Drupal project.</p>
<p>I cloned a fresh version of my <a href="https://github.com/opdavies/docker-examples">Docker Examples repository</a> and started the Drupal example.</p>
<p>I ran <code>mkdir -p web/modules/custom/example/tests/src/Functional</code> to create the directory structure that I needed, and then <code>touch web/modules/custom/example/tests/src/Functional/ExampleTest.php</code> to create a new test file and populated it with some initial code:</p>
<pre><code class="language-php"><?php
namespace Drupal\Tests\example\Functional;
use Drupal\Tests\BrowserTestBase;
use Symfony\Component\HttpFoundation\Response;
class ExampleTest extends BrowserTestBase {
protected $defaultTheme = 'stark';
}
</code></pre>
<p>For the simplest test, I decided to test some existing Drupal core functionality - that an anonymous user can view the front page:</p>
<pre><code class="language-php">/** @test */
public function the_front_page_loads_for_anonymous_users() {
<p>To execute the test, I ran <code>SIMPLETEST_DB=sqlite://localhost//dev/shm/test.sqlite SIMPLETEST_BASE_URL=http://web phpunit -c web/core web/modules/custom</code>. The environment variables could be added to a <code>phpunit.xml.dist</code> file but I decided to add them to the command and use Drupal core's PHPUnit configuration file.</p>
<p>As this is existing functionalty, the test passes. I can change either the path or the response code to ensure it also fails when expected.</p>
<p>With the first test working, it's easy to add more for other functionality, such as whether different users should be able to access administration pages:</p>
<pre><code class="language-php">/** @test */
public function the_admin_page_is_not_accessible_to_anonymous_users() {
<p>Hopefully, this shows how quickly you can get tests running for a Drupal module. If you'd like to see more, the slides and video recording of my <a href="/presentations/tdd-test-driven-drupal">Test-Driven Drupal talk</a> are online.</p>