Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Jonjie's avatar
Level 12

Method naming convention when writing tests

Please don't hate me, I'm just wondering what convention are you using today when you're writing tests, do you guys use the snake_case or the camelCase? Larry (bot) said it is best practice to use camelCase, but I just want to know your thoughts about this.

0 likes
9 replies
Chirag4ACQ's avatar

When writing tests in Laravel, it's essential to follow a consistent method naming convention to make your test code organized and readable. Laravel follows the PHPUnit testing framework, which itself has established naming conventions for test methods. Here's how you should name your test methods in Laravel:

Test Method Prefix: Test methods should start with the word "test." This prefix is essential for PHPUnit to recognize them as test methods.

Method Being Tested: After "test," you should include the name of the method or functionality you're testing. Use a clear and descriptive name that conveys the purpose of the test. For example, if you're testing a method named calculateTotal(), your test method might be named testCalculateTotal().

CamelCase for Test Method Names: Use camelCase to separate words in the method name. For example, testCalculateTotal() uses camelCase.

use Tests\TestCase;

class OrderTest extends TestCase { public function testCalculateTotal() { // Your test code goes here }

public function testApplyDiscount()
{
    // Your test code goes here
}

}

By adhering to this naming convention, your test methods will be more organized and easy to understand, making it clear which parts of your codebase are being tested and what specific functionality is being verified. This convention also helps Laravel's testing framework automatically discover and run your tests.

1 like
Jonjie's avatar
Level 12

@Chirag4ACQ Thanks for your inputs bro. I prefer using docblock when writing methods tho instead of adding the word test infront of method name, like so:

/** @test */
public function it_delivers_a_receipt(){}
Tray2's avatar

When using phpunit I usually use snake case a_user_is_allowed_create_a_post, but since switching to pest. I no longer need to do that.

it('allows a user to create a post', function() {
	//Testcode
});
1 like
Jonjie's avatar
Level 12

@Tray2 Are you totally switching to pest? Like I mean if by chance you'll have a new project, would you use pest on it?

tisuchi's avatar

@jonjie Both look good to me.

If you use phpunit, I personally prefer snake case because this convention is more popular in the php community.

3 likes
martinbean's avatar

@jonjie I use camelCase, but seem to be in the minority.

My opinion is: I don’t understand why developers will follow PSRs for “application” code (which includes using camelCase for using method names) but then throw that out of the window for tests.

The argument is, “it’s more readable.” Well, if snake_case is more readable, why did the PHP-FIG decide on camelCase for method naming instead? I personally don’t have trouble distinguishing words written in camelCase, but I guess I also have the benefit of being a native English speaker.

1 like
Talinon's avatar

@martinbean You raise a valid point. I'd say the difference would be application method names are relatively short compared to test function names which can be long and descriptive. I just see snake_case as a way to relieve developer fatigue. TDD is a large part of my day, and anything to make the process a bit easier on the eyes is a win.

2 likes
MartynasAl's avatar

Consistency based on context.

For code, camelCase. Usually you try to make your method names short and to the point.

For tests, snake_case. Method names for tests tend to be a lot longer and more descriptive. Extra spacing, that underscores bring, just makes it more easier to identify different words. Better readability, less developer fatigue.

1 like

Please or to participate in this conversation.