不使用驼峰命名法命名 PHPUnit 测试方法会产生什么后果?
我正在使用 PHPUnit 编写单元测试。
我现在已经编写了大约100 个方法。
由于这些是针对 Kohana 框架应用程序的,因此我也使用其命名约定作为测试方法,例如:
function test_instance()
{
...
}
function test_config()
{
...
}
我的所有测试运行良好 Eclipse 和命令行。
但是,我现在需要使用 setup 函数,并意识到它仅在命名时有效:
function setUp()
{
...
}
而不是:
function set_up()
{
...
}
现在我想知道如果我不这样做,将来是否会有任何缺点重命名我的所有 PHPUnit 方法,使它们采用驼峰式命名法,例如,使用 PHPUnit 的其他工具是否不使用驼峰式命名法?
I'm writing unit tests with PHPUnit.
I now have about 100 methods written.
Since these are for a Kohana framework application, I used its naming convention for the test methods as well, e.g.:
function test_instance()
{
...
}
function test_config()
{
...
}
All my tests run fine in Eclipse and from the command line.
However, I now need to use a setup function and realized that it only works when named:
function setUp()
{
...
}
and not:
function set_up()
{
...
}
Now I am wondering if I will have any disadvantages down the road if I don't rename all of my PHPUnit methods so that they are camel case, e.g. do other tools that use PHPUnit excect the method names to be in camelcase notation?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
PHP 中的方法名称不区分大小写,因此使用
setup
或setUp
以及tearDown
或teardown 并不重要
。但是,您不能使用set_up
或tear_down
,因为那是不同的名称。 PHPUnit 在每次测试之间调用setUp
和tearDown
来保证环境。按照惯例,PHPUnit 认为任何以
test
开头的方法都是测试。您可以通过在方法 DocBlock 中使用@test
注释来省略前缀。在打印任何报告时,PHPUnit 会将 CamelCased 方法名称转换为空格描述,因此testMethodDoesThis
将显示为“MethodDoesThis”。CamelCase 还会影响使用
@testDox
注释进行注释的测试。Method names in PHP are not case-sensitive, so it doesn't matter whether you use
setup
orsetUp
andtearDown
orteardown
. However, you may not useset_up
ortear_down
, because that is a different name. PHPUnit callssetUp
andtearDown
between each test to guarantee the environment.By convention PHPUnit considers any method starting with
test
to be a Test. You can omit the prefix by using the@test
annotation in the method DocBlock. PHPUnit will transform CamelCased method names to spaced descriptions when printing any reports, sotestMethodDoesThis
will appear as "Method Does This".CamelCase will also affect tests annotated with the
@testDox
annotation.setUp
和teardown
是必须匹配才能被识别的确切名称。另一方面,测试只需以test
开始。 (所有这些都区分大小写。)除此之外,对其余命名的大小写/布局没有要求。 (嗯,类不能以
Test
开头,但仅此而已。)setUp
andteardown
are exact names that have to be matched in order to be recognized. Tests, on the other hand, just have to begin withtest
. (All of these are case-sensitive.)Aside from that, no requirements on the capitalization/layout of the rest of naming. (Well, classes can't start with
Test
, but that's about it.)