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:

public function testSomethingHappensWhenYouGoToThePage()

Some people find long camel-case names hard to read and prefer to use snake-case names:

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:

/** @test */
public function something_happens_when_you_go_to_the_page()

And in newer PHPUnit versions, you can also use an attribute:

#[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?

- Oliver

Was this interesting?

Sign up here and get more like this delivered straight to your inbox every day.

About me

Picture of Oliver

I'm an Acquia-certified Drupal Triple Expert with 17 years of experience, an open-source software maintainer and Drupal core contributor, public speaker, live streamer, and host of the Beyond Blocks podcast.