简体   繁体   English

带有驼峰大小写或下划线的PHPUnit测试类

[英]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: 在PHPUnit遵循的xUnit样式中编写测试用例时,似乎每个人都遵循函数名称的camel case约定:

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

I have been naming my methods with a more "eloquent" PHPSpec style: 我一直用更“雄辩”的PHPSpec风格命名我的方法:

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. 就个人而言,我发现它更具可读性,更容易回归。

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* 测试是名为test*公共方法

PHPUnit will treat it as a test. PHPUnit会将其视为测试。

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). PHPUnit会将camel cased函数名称转换为适当间隔的输出描述,因此test_it_has_a_fluent_interface将显示为test it has a fluent interface (仅使用版本4.0.17和4.4.1进行测试)。

Alternatively, you can use the @test annotation in a method's docblock to mark it as a test method. 或者,您可以在方法的docblock中使用@test注释将其标记为测试方法。

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. 这也为您提供了测试方法体的结构描述。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM