Search code examples
unit-testingtestingphpunitxunitphpspec

PHPUnit test classes with camel case or underscore


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.


Solution

  • 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.