123 lines
5.4 KiB
YAML
123 lines
5.4 KiB
YAML
uuid:
|
|
- value: 2e8662f9-eab9-41a9-ae4a-7145b7931bea
|
|
langcode:
|
|
- value: en
|
|
type:
|
|
- target_id: daily_email
|
|
target_type: node_type
|
|
target_uuid: 8bde1f2f-eef9-4f2d-ae9c-96921f8193d7
|
|
revision_timestamp:
|
|
- value: '2025-07-15T21:42:41+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: 'The downside to testing existing sites'
|
|
created:
|
|
- value: '2025-07-14T21:24:24+00:00'
|
|
changed:
|
|
- value: '2025-07-15T21:42:41+00:00'
|
|
promote:
|
|
- value: false
|
|
sticky:
|
|
- value: false
|
|
default_langcode:
|
|
- value: true
|
|
revision_translation_affected:
|
|
- value: true
|
|
path:
|
|
- alias: /daily/2025/07/14/downside-testing-existing-sites
|
|
langcode: en
|
|
body:
|
|
- value: |-
|
|
Whilst [experimenting with Drupal Test Traits][1], I've needed to re-think how I've written some tests.
|
|
|
|
When writing a test for a Repository class that finds published podcast episode nodes, I can't create three published and two unpublished nodes, get the result and assert exactly three were returned.
|
|
|
|
Because I'm testing an existing site, all my existing content is available in each test.
|
|
|
|
I already have published podcast episode nodes, so the count is always going to be greater than what I created in the test.
|
|
|
|
In a traditional test, everything is re-installed from scratch for every test, so this wouldn't be an issue.
|
|
|
|
## The issue testing existing sites
|
|
|
|
If I'm testing an existing site and already have a page with the path of `/available`, this test would pass:
|
|
|
|
```php
|
|
<?php
|
|
|
|
/** @test */
|
|
public function the_available_page_loads(): void {
|
|
$this->drupalGet('/available');
|
|
|
|
$assert = $this->assertSession();
|
|
$assert->statusCodeEquals(Response::HTTP_OK);
|
|
$assert->responseContains('Available for Drupal consulting');
|
|
}
|
|
```
|
|
|
|
But, there's nothing in the test to show the page was created.
|
|
|
|
The test assumes the page already exists, but how do we know that?
|
|
|
|
Can we assume the page will always exist?
|
|
|
|
Will it exist on every development environment or in a CI pipeline?
|
|
|
|
## Here's the thing
|
|
|
|
Maybe it's against the spirit of testing an existing site, but my advice is to only assert on what you've created in that test.
|
|
|
|
It makes the test more readable and easier to understand.
|
|
|
|
People don't need to go elsewhere to find how something was created.
|
|
|
|
If I'm testing published podcast nodes, I can count how many there are at the start of the test and assert the number has increased by a given amount and get the same result.
|
|
|
|
The test just needs to be written slightly differently.
|
|
|
|
Or, you can mix and match, and [use traditional test types where appropriate][0].
|
|
|
|
[0]: /daily/2025/07/13/drupal-test-traits-not-replacement-traditional-tests
|
|
[1]: /daily/2025/06/18/exploring-drupal-test-traits
|
|
format: markdown
|
|
processed: |
|
|
<p>Whilst <a href="http://localhost:8888/daily/2025/06/18/exploring-drupal-test-traits">experimenting with Drupal Test Traits</a>, I've needed to re-think how I've written some tests.</p>
|
|
<p>When writing a test for a Repository class that finds published podcast episode nodes, I can't create three published and two unpublished nodes, get the result and assert exactly three were returned.</p>
|
|
<p>Because I'm testing an existing site, all my existing content is available in each test.</p>
|
|
<p>I already have published podcast episode nodes, so the count is always going to be greater than what I created in the test.</p>
|
|
<p>In a traditional test, everything is re-installed from scratch for every test, so this wouldn't be an issue.</p>
|
|
<h2>The issue testing existing sites</h2>
|
|
<p>If I'm testing an existing site and already have a page with the path of <code>/available</code>, this test would pass:</p>
|
|
<pre><code><?php
|
|
|
|
/** @test */
|
|
public function the_available_page_loads(): void {
|
|
$this->drupalGet('/available');
|
|
|
|
$assert = $this->assertSession();
|
|
$assert->statusCodeEquals(Response::HTTP_OK);
|
|
$assert->responseContains('Available for Drupal consulting');
|
|
}
|
|
</code></pre><p>But, there's nothing in the test to show the page was created.</p>
|
|
<p>The test assumes the page already exists, but how do we know that?</p>
|
|
<p>Can we assume the page will always exist?</p>
|
|
<p>Will it exist on every development environment or in a CI pipeline?</p>
|
|
<h2>Here's the thing</h2>
|
|
<p>Maybe it's against the spirit of testing an existing site, but my advice is to only assert on what you've created in that test.</p>
|
|
<p>It makes the test more readable and easier to understand.</p>
|
|
<p>People don't need to go elsewhere to find how something was created.</p>
|
|
<p>If I'm testing published podcast nodes, I can count how many there are at the start of the test and assert the number has increased by a given amount and get the same result.</p>
|
|
<p>The test just needs to be written slightly differently.</p>
|
|
<p>Or, you can mix and match, and <a href="http://localhost:8888/daily/2025/07/13/drupal-test-traits-not-replacement-traditional-tests">use traditional test types where appropriate</a>.</p>
|
|
summary: ''
|
|
field_daily_email_cta:
|
|
- target_type: node
|
|
target_uuid: e3f6c728-7855-4804-8614-e2a0c08c368f
|