PHPUnit test classes with camel case or underscore

When writing xUnit style test cases that follow PHPUnit, it seems that each follows the camel case convention for function names:

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

      

I named my methods in the more "eloquent" style of PHPSpec:

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

      

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

+3


source to share


2 answers


Generally speaking: No, it won't give you any problems at the moment (I don't see the future, so I'm not sure how this answer will be true, say, ten years from now!).

Referring to the manual while

tests are publicly available methods called test*



PHPUnit will treat it like a test.

PHPUnit will convert camel function names to properly spaced descriptions for output, so it test_it_has_a_fluent_interface

will display as test it has a fluent interface

(just tested with versions 4.0.17 and 4.4.1).

Alternatively, you can use annotation @test

in the docblock method to mark it as a test method.

+2


source


Your more eloquent style won't change much. It's still a drop of words, this time with additional separation. Instead of doing this, I propose a more contextual approach based on the following pattern:

methodUnderTest_ExpectedResultOrBehavior_OptionalConditions_OptionalContext

      

Your example would be as follows:



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

      

It also gives you a structural description of the body of the test method.

0


source







All Articles