Summer Sale! All accounts are 50% off this week.

martinbean's avatar

Naming convention for PHPUnit tests in Laravel

Until the start of the year, I was a freelance developer for a long period of time which meant mainly working alone on projects, so I (naïvely) didn’t see the benefits of testing. But in January, I took a role as a senior developer and decided to properly embrace testing. I’m beginning to enjoy it and not see it as a chore.

The project I’m currently working is tested using Behat, so I love the fluent “integration” test-like methods Laravel adds to PHPUnit. But one thing I struggle with is naming. With Laravel not using PHPUnit specifically for unit testing components, how do you guys name your test files and the cases within for your integration tests?

0 likes
3 replies
ifpingram's avatar

The files depend; by class often, but also by feature, e.g.

UserSearchesForPropertiesForSaleTest.php

Regards the test case names, I name them as precisely (not concisely) as possible, but this often means the names are super long. For example, some of the ridiculously long ones are:

testEuroCurrencySelectionIsDisplayedInThePropertySearchIfItHasBeenSelected()

testNonExistentRegionsAndDepartmentsSearchCriteriaAreNotReturnedAsASearchLocation()

testSearchWithMinimumPriceCriteriaThatReturns0ResultsDisplaysALinkToASearchWithMinimumPriceCriteriaDecreasedBy25Percent()

The last one is about as long as they get. :D I then use the PHPUnit --testdox flag to make sense of them as living documentation, so I get the output:

[ ] Search with minimum price criteria that returns 0 results displays a link to a search with minimum price criteria decreased by 25 percent

Obviously, the above tests are for integration type tests hitting multiple layers. If my lower level unit tests were named so long, this would be a huge code smell that the unit is doing far too much...

5 likes
martinbean's avatar

@ifpingram Thanks for that. Like you I'm using PHPUnit in a personal project for integration-like tests. So I have say, a test named AuthenticationTest that contains cases like testUserCanLogInWithValidCredentials(), testRegistrationWithDuplicateEmail(), and so on. Was just looking for a bit of validation that this was the expected way to go and seems so from your answer, so thanks for that!

Please or to participate in this conversation.