oliverdavies.uk/source/_daily_emails/2024-10-22.md
Oliver Davies feb93810bc Add daily email for 2024-10-22
How would you write this test name?
2024-10-24 12:52:19 +01:00

51 lines
1.3 KiB
Markdown

---
title: How would you write this test name?
date: 2024-10-22
permalink: daily/2024/10/22/how-would-you-write-this-test-name
tags:
- software-development
- php
- phpunit
- automated-testing
- test-driven-development
cta: ~
snippet: |
How would you write this test name?
---
There are multiple ways I've seen people write their test method names.
This is the standard PSR-compliant camel-case method name:
```php
public function testSomethingHappensWhenYouGoToThePage()
```
Some people find long camel-case names hard to read and prefer to use snake-case names:
```php
public function test_something_happens_when_you_go_to_the_page()
```
This still works as the method name still starts with the word `test`, but you'd need to add some overrides to phpcs for it not to complain about using snake-case words.
Another option is to remove the `test` prefix and use an annotation:
```php
/** @test */
public function something_happens_when_you_go_to_the_page()
```
And in newer PHPUnit versions, you can also use an attribute:
```php
#[Test]
public function something_happens_when_you_go_to_the_page()
```
Whilst this makes the method name shorter, you need to add an additional line before each test method for the annotation or attribute.
Each has pros and cons, and people have their own preferences.
Which do you do?