oliverdavies.uk/source/_daily_emails/2023-08-17.md

30 lines
840 B
Markdown
Raw Normal View History

2024-01-03 20:00:00 +00:00
---
title: >
2024-02-07 20:01:19 +00:00
Writing custom assertions in your tests
2024-01-03 20:00:00 +00:00
pubDate: 2023-08-17
permalink: >-
2024-02-07 20:01:19 +00:00
archive/2023/08/17/writing-custom-assertions-in-your-tests
2024-01-03 20:00:00 +00:00
tags:
2024-02-07 20:01:19 +00:00
- automated-testing
- test-driven-development
2024-01-03 20:00:00 +00:00
---
Writing custom assertions is a great way to clean up your test code.
Here's an example from one of my client Drupal projects:
```language-php
2024-01-03 20:00:00 +00:00
private static function assertProductVariationHasPrice(ProductVariationInterface $productVariation, string $expectedPrice): void {
self::assertSame(
actual: $productVariation->getPrice()->getNumber(),
expected: $expectedPrice,
);
}
```
This one wraps a single assertion, but they could also include multiple assertions or additional steps.
## Here's the thing
A custom assertion is a simple function but it makes the test code more readable and less repetitive.