oliverdavies.uk/content/node.a1d0e52d-4cfc-4695-97f8-7f8a4c1a2938.yml

117 lines
5.9 KiB
YAML

uuid:
- value: a1d0e52d-4cfc-4695-97f8-7f8a4c1a2938
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: |
Which type of test should I use?
created:
- value: '2023-09-13T00: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/13/which-type-of-test-should-i-use
langcode: en
body:
- value: |
<p>Drupal offers a few different types of tests you can use, but how do you know which one to pick?</p>
<p>This is how I decide based on what I need to test by asking these questions:</p>
<h2 id="do-i-need-to-test-http-requests-and-responses%3F">Do I need to test HTTP requests and responses?</h2>
<p>Do I need to make GET or POST requests to a URL and check its response code, e.g. to check the endpoint exists, that I get an expected "Page not found" or "Forbidden" message, or to check if an item was created or updated?</p>
<p>If so, use a <code>Functional</code> test like <code>BrowserTestBase</code>.</p>
<h2 id="do-i-need-to-test-with-javascript%3F">Do I need to test with JavaScript?</h2>
<p><code>BrowserTestBase</code> doesn't include JavaScript, so use a <code>FunctionaJavaScript</code> test if needed.</p>
<h2 id="do-i-need-to-retrieve-services-from-the-service%2Fdependency-injection-container%3F">Do I need to retrieve services from the service/Dependency Injection container?</h2>
<p>If I don't need to make HTTP requests but need access to the service container, I use a Kernel test such as <code>KernelTestBase</code>.</p>
<p>There's more setup for kernel tests compared to functional tests, such as installing the required configuration and creating schema tables in the database. However, having less installed means the tests run faster.</p>
<p>There are different base classes, such as <code>EntityKernelTestBase</code>, that can be used depending on the situation to simplify the setup.</p>
<h2 id="can-i-test-the-code-in-isolation-without-the-service-container%3F">Can I test the code in isolation without the service container?</h2>
<p>If I can test the code without needing the service container and mock simple dependencies, then use a <code>Unit</code> test.</p>
<p>These are very fast to run, but you can overuse mocks and sometimes test the mocks instead of the code you intended to test.</p>
<h2 id="here%27s-the-thing">Here's the thing</h2>
<p>As well as the regular unit test type, Drupal offers various types of test that can be used depending on the situation.</p>
<p>I like to start with functional tests first and switch to kernel or unit tests when needed. They're slower to run but easier to write as they need less setup and arguably provide the most value.</p>
<p>If you're just getting started with testing, they're probably the quickest to learn too.</p>
format: full_html
processed: |
<p>Drupal offers a few different types of tests you can use, but how do you know which one to pick?</p>
<p>This is how I decide based on what I need to test by asking these questions:</p>
<h2 id="do-i-need-to-test-http-requests-and-responses%3F">Do I need to test HTTP requests and responses?</h2>
<p>Do I need to make GET or POST requests to a URL and check its response code, e.g. to check the endpoint exists, that I get an expected "Page not found" or "Forbidden" message, or to check if an item was created or updated?</p>
<p>If so, use a <code>Functional</code> test like <code>BrowserTestBase</code>.</p>
<h2 id="do-i-need-to-test-with-javascript%3F">Do I need to test with JavaScript?</h2>
<p><code>BrowserTestBase</code> doesn't include JavaScript, so use a <code>FunctionaJavaScript</code> test if needed.</p>
<h2 id="do-i-need-to-retrieve-services-from-the-service%2Fdependency-injection-container%3F">Do I need to retrieve services from the service/Dependency Injection container?</h2>
<p>If I don't need to make HTTP requests but need access to the service container, I use a Kernel test such as <code>KernelTestBase</code>.</p>
<p>There's more setup for kernel tests compared to functional tests, such as installing the required configuration and creating schema tables in the database. However, having less installed means the tests run faster.</p>
<p>There are different base classes, such as <code>EntityKernelTestBase</code>, that can be used depending on the situation to simplify the setup.</p>
<h2 id="can-i-test-the-code-in-isolation-without-the-service-container%3F">Can I test the code in isolation without the service container?</h2>
<p>If I can test the code without needing the service container and mock simple dependencies, then use a <code>Unit</code> test.</p>
<p>These are very fast to run, but you can overuse mocks and sometimes test the mocks instead of the code you intended to test.</p>
<h2 id="here%27s-the-thing">Here's the thing</h2>
<p>As well as the regular unit test type, Drupal offers various types of test that can be used depending on the situation.</p>
<p>I like to start with functional tests first and switch to kernel or unit tests when needed. They're slower to run but easier to write as they need less setup and arguably provide the most value.</p>
<p>If you're just getting started with testing, they're probably the quickest to learn too.</p>
summary: null
field_daily_email_cta: { }