hen*_*ght 5 php phpunit unit-testing
在PHPUnit的手动亮点一些约定:
MyClass
进入一堂课MyClassTest
MyClassTest
直播文件MyClassTest.php
MyClassTest
继承自 PHPUnit_Framework_TestCase
test*
这将导致类似于以下文件夹结构:
??? src/
? ??? classes/
? ? ??? MyClass.php # Different
? ??? ...
??? tests/
? ??? testcases/
? ? ??? MyClassTest.php # Different
? ??? bootstrap.php
? ??? ...
??? ...
Run Code Online (Sandbox Code Playgroud)
...以及这个测试案例:
MyClassTest extends PHPUnit_Framework_TestCase {
testMyMethod() {
// Code here.
}
}
Run Code Online (Sandbox Code Playgroud)
我的问题
我想知道是否有任何原因导致测试套件中使用的命名无法反映项目的源代码?例如,我认为文件名可以匹配:
??? src/
? ??? classes/
? ? ??? MyClass.php # Same
? ??? ...
??? tests/
? ??? testcases/
? ? ??? MyClass.php # Same
? ??? bootstrap.php
? ??? ...
??? ...
Run Code Online (Sandbox Code Playgroud)
如果使用PHP> 5.3,则可以使用名称空间来匹配类名称:
namespace MyProject\MyTests;
MyClass extends PHPUnit_Framework_TestCase { # The class name MyClass matches the class name used in my project's source.
/**
* @test
*/
MyMethod() { # The method name MyMethod matches the method name used in my project's source.
// Code here.
}
}
Run Code Online (Sandbox Code Playgroud)
注意,@tests
使用了注释,以便方法名称可以匹配。
如果使用 PHP > 5.3,则可以使用命名空间来允许类名匹配:
不这样做的原因如下:
否则,您需要使用类别名导入被测试的类,以将其与测试用例区分开来:
use MyProject\MyClass as MyActualClass;
Run Code Online (Sandbox Code Playgroud)方法名称 MyMethod 与我的项目源中使用的方法名称相匹配。
如果您认为这testMyMethod
是替代方案,这可能听起来很有吸引力,但这不是惯例。相反,您应该使用更具描述性的测试方法名称,例如testThatMyMethodReturnsTrueIfFooIsBar
.
归档时间: |
|
查看次数: |
3917 次 |
最近记录: |