Add daily email for 2023-11-27
Finding the best test base
This commit is contained in:
parent
bf16d1f548
commit
430fe5eac7
1 changed files with 99 additions and 0 deletions
99
src/content/daily-email/2023-11-27.md
Normal file
99
src/content/daily-email/2023-11-27.md
Normal file
|
|
@ -0,0 +1,99 @@
|
||||||
|
---
|
||||||
|
title: >
|
||||||
|
Finding the best test base
|
||||||
|
pubDate: 2023-11-27
|
||||||
|
permalink: >
|
||||||
|
archive/2023/11/27/finding-the-best-test-base
|
||||||
|
tags:
|
||||||
|
- software-development
|
||||||
|
- automated-testing
|
||||||
|
- test-driven-development
|
||||||
|
- php
|
||||||
|
- drupal
|
||||||
|
---
|
||||||
|
|
||||||
|
As well as different base classes for types of tests - i.e. functional, kernel and unit - there are other test base classes within those that can be used to simplify things.
|
||||||
|
|
||||||
|
For example, if we have this test:
|
||||||
|
|
||||||
|
```php
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Drupal\Tests\example\Kernel;
|
||||||
|
|
||||||
|
use Drupal\KernelTests\KernelTestBase;
|
||||||
|
use Drupal\Tests\node\Traits\NodeCreationTrait;
|
||||||
|
use Drupal\Tests\user\Traits\UserCreationTrait;
|
||||||
|
|
||||||
|
class ExampleTest extends KernelTestBase {
|
||||||
|
|
||||||
|
use NodeCreationTrait;
|
||||||
|
use UserCreationTrait;
|
||||||
|
|
||||||
|
protected static $modules = [
|
||||||
|
'node',
|
||||||
|
'user',
|
||||||
|
];
|
||||||
|
|
||||||
|
public function setUp(): void {
|
||||||
|
parent::setUp();
|
||||||
|
|
||||||
|
$this->installEntitySchema(entity_type_id: 'node');
|
||||||
|
$this->installEntitySchema(entity_type_id: 'user');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testExample(): void {
|
||||||
|
$user = $this->createUser();
|
||||||
|
|
||||||
|
$article = $this->createNode([
|
||||||
|
'title' => 'Test article',
|
||||||
|
'uid' => $user,
|
||||||
|
]);
|
||||||
|
|
||||||
|
self::assertSame('1', $article->getOwnerId());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
Both creation traits must be imported, the `node` and `user` modules must be enabled, and the entity tables must be installed.
|
||||||
|
|
||||||
|
When writing a lot of tests, this can result in duplication and more complex tests that take longer to write.
|
||||||
|
|
||||||
|
This can be simplified using `EntityKernelTestBase` instead of `KernelTestBase`:
|
||||||
|
|
||||||
|
```php
|
||||||
|
<?php
|
||||||
|
|
||||||
|
use Drupal\KernelTests\Core\Entity\EntityKernelTestBase;
|
||||||
|
use Drupal\Tests\node\Traits\NodeCreationTrait;
|
||||||
|
|
||||||
|
class ExampleTest extends EntityKernelTestBase {
|
||||||
|
|
||||||
|
use NodeCreationTrait;
|
||||||
|
|
||||||
|
protected static $modules = [
|
||||||
|
'node',
|
||||||
|
];
|
||||||
|
|
||||||
|
public function testExample(): void {
|
||||||
|
$user = $this->createUser();
|
||||||
|
|
||||||
|
$article = $this->createNode([
|
||||||
|
'title' => 'Test article',
|
||||||
|
'uid' => $user,
|
||||||
|
]);
|
||||||
|
|
||||||
|
self::assertSame('1', $article->getOwnerId());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
The class is simpler, fewer modules must be specified, and the entity schemas are automatically installed.
|
||||||
|
|
||||||
|
As well as the core modules, some contrib modules also provide their own base test cases.
|
||||||
|
|
||||||
|
If you're using the Webform module, you may want to use `WebformAccessTestBase` instead of the standard `UnitTestCase`.
|
||||||
|
|
||||||
|
It's definitely worth spending some time looking at what base test cases are available and which are the best ones to use for your own tests.
|
||||||
Loading…
Add table
Add a link
Reference in a new issue