4

When writing test cases inthe xUnit style that PHPUnit follows it seems that everyone follows the camel case convention for function names:

public function testObjectHasFluentInterface()
{
    // ...
}

I have been naming my methods with a more "eloquent" PHPSpec style:

public function test_it_has_a_fluent_interface()
{
    // ...
}

Will this style create problems for me in the future? Personally I find it vastly more readable and easy to come back to.

AndrewMcLagan
  • 13,459
  • 23
  • 91
  • 158

2 Answers2

4

Generally speaking: No, it currently won't cause you any problems (I can't see the future, so I'm not sure how this answer will be true in lets say about ten years from now!).

Referring to the manual, as long as

the tests are public methods that are named test*

PHPUnit will treat it as a test.

PHPUnit will transform camel cased function names to properly spaced descriptions for output, so test_it_has_a_fluent_interface will appear as test it has a fluent interface (just tested with version 4.0.17 and 4.4.1).

Alternatively, you can use the @test annotation in a method's docblock to mark it as a test method.

Bjoern
  • 15,934
  • 4
  • 43
  • 48
0

Your more eloquent style doesn't change much. It's still a blob of words, this time with extra separation. Instead of doing either, I suggest more contextual approach based on following template:

methodUnderTest_ExpectedResultOrBehavior_OptionalConditions_OptionalContext

Your example(s) would be then:

public function testObject_HasFluentInterface
public function saveSale_ThrowsException_WhenTransactionDateIsYesterday
public function calculatePrice_ReturnsPrice_CalculatedIncludingPromotion
public function generateXml_CreatesXml_AndSavesItToFile_WhenAtLeastOneEntityExists

This also gives you sort of structural description of test method body.

k.m
  • 30,794
  • 10
  • 62
  • 86
  • 1
    Personally I find this incredibly unreadable, hard to maintain, and confusing for future developers. Seems like you are describing your tests in the function name: Your test code should read like a story, you should not need this level of verbosity. It actually is dangerous in the sense it create a point of maintenance: one day a developer will 100% update the test and not rename the function. BOOM yuk. – AndrewMcLagan Mar 29 '19 at 04:08
  • Tests must start with `test`, so I don't see how the last three methods are valid examples. – Leo Galleguillos Feb 28 '20 at 06:05