oliverdavies.uk/content/node.0a3bf034-7203-45c2-aae5-74f9211929fa.yml

136 lines
4.5 KiB
YAML
Raw Normal View History

2025-07-10 00:14:12 +01:00
uuid:
- value: 0a3bf034-7203-45c2-aae5-74f9211929fa
langcode:
- value: en
type:
- target_id: daily_email
target_type: node_type
target_uuid: 8bde1f2f-eef9-4f2d-ae9c-96921f8193d7
revision_timestamp:
- value: '2025-05-11T09:00:32+00:00'
revision_uid:
- target_type: user
target_uuid: b8966985-d4b2-42a7-a319-2e94ccfbb849
revision_log: { }
status:
- value: true
uid:
- target_type: user
target_uuid: b8966985-d4b2-42a7-a319-2e94ccfbb849
title:
- value: |
What's the simplest test to begin with?
created:
- value: '2023-09-07T00:00:00+00:00'
changed:
- value: '2025-05-11T09:00:32+00:00'
promote:
- value: false
sticky:
- value: false
default_langcode:
- value: true
revision_translation_affected:
- value: true
path:
- alias: /daily/2023/09/07/what-s-the-simplest-test-to-begin-with
langcode: en
body:
- value: |
<p>When giving talks and workshops or coaching on automated testing and test-driven development, some people may not have written tests before and aren't familiar with the structure or know where to begin.</p>
<p>In the workshops I ran for DrupalCamp London and DrupalCamp NYC, I wanted to cover this first before writing any implementation code.</p>
<p>Where do you put a test class, and what does it contain?</p>
<p>How do you run the tests, and how can you make it pass or fail?</p>
<h2 id="what-we-did">What we did</h2>
<p>To start, we wrote a test for existing functionality within Drupal core - anonymous users can visit the front page.</p>
<p>This is the whole test:</p>
<pre><code class="language-php">&lt;?php
namespace Drupal\Tests\my_module\Functional;
use Drupal\Tests\BrowserTestBase;
use Symfony\Component\HttpFoundation\Response;
class MyModuleTest extends BrowserTestBase {
  protected $defaultTheme = 'stark';
  /** @test */
  public function the_front_page_loads_for_anonymous_users() {
    $this-&gt;drupalGet('&lt;front&gt;');
    $this-&gt;assertResponse(Response::HTTP_OK);
  }
}
</code></pre>
<p>This is a test someone can write, run and see the test pass.</p>
<p>They can then experiment by changing the values to make the test fail in different ways.</p>
<h2 id="what-next%3F">What next?</h2>
<p>Then, we tested anonymous users cannot access the administration pages, which is also already the case in Drupal core, and then authenticated users with the correct permissions could access them.</p>
<p>People were getting the idea by now, and we moved on to writing and testing some of our own code.</p>
format: full_html
processed: |
<p>When giving talks and workshops or coaching on automated testing and test-driven development, some people may not have written tests before and aren't familiar with the structure or know where to begin.</p>
<p>In the workshops I ran for DrupalCamp London and DrupalCamp NYC, I wanted to cover this first before writing any implementation code.</p>
<p>Where do you put a test class, and what does it contain?</p>
<p>How do you run the tests, and how can you make it pass or fail?</p>
<h2 id="what-we-did">What we did</h2>
<p>To start, we wrote a test for existing functionality within Drupal core - anonymous users can visit the front page.</p>
<p>This is the whole test:</p>
<pre><code class="language-php">&lt;?php
namespace Drupal\Tests\my_module\Functional;
use Drupal\Tests\BrowserTestBase;
use Symfony\Component\HttpFoundation\Response;
class MyModuleTest extends BrowserTestBase {
&nbsp; protected $defaultTheme = 'stark';
&nbsp; /** @test */
&nbsp; public function the_front_page_loads_for_anonymous_users() {
&nbsp; &nbsp; $this-&gt;drupalGet('&lt;front&gt;');
&nbsp; &nbsp; $this-&gt;assertResponse(Response::HTTP_OK);
&nbsp; }
}
</code></pre>
<p>This is a test someone can write, run and see the test pass.</p>
<p>They can then experiment by changing the values to make the test fail in different ways.</p>
<h2 id="what-next%3F">What next?</h2>
<p>Then, we tested anonymous users cannot access the administration pages, which is also already the case in Drupal core, and then authenticated users with the correct permissions could access them.</p>
<p>People were getting the idea by now, and we moved on to writing and testing some of our own code.</p>
summary: null
field_daily_email_cta: { }