In less than 12 months, Drupal 7 will be end-of-life and no longer supported. Plan your upgrade to Drupal 10 now.

Writing good test names

In PHPUnit, there are different ways to write test methods.

The standard way is to use camel-case method names with a test prefix, for example:

public function testTheProjectNameShouldBeAString(): void
{
    // ...
}

Another popular way, particularly in some frameworks, is to use snake-case method names:

/** @test */
public function the_project_name_should_be_a_string(): void
{
    // ...
}

This may be more readable but only works if the @test annotation is present.

What if you need to add another annotation, such as @dataProvider to the test? Do you do multi-line docblocks everywhere, or mix and match single and multiple line docblocks depending on the test?

Here's the thing

My preference switches between both ways of writing test methods.

But, whichever way I write it, I write descriptive method names that explain what the test does - even if it means having a long method name.

I avoid short and generic names like testUpdate().

People should be able to read the test name and understand what it does and what it's testing.

- Oliver

P.S. Do you need immediate access to an expert Drupal Developer? With my Drupal development subscription, make unlimited requests for a fixed monthly price in less time than posting to a job board!

Was this useful?

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.