PHPUnit测试套件命名约定
PHPUnit manual强调了一些惯例:
> MyClass类的测试进入MyClassTest类 这将导致类似于此文件夹结构: ├── src/ │ ├── classes/ │ │ ├── MyClass.php # Different │ └── ... ├── tests/ │ ├── testcases/ │ │ ├── MyClassTest.php # Different │ ├── bootstrap.php │ └── ... └── ... ……和这个测试用例: MyClassTest extends PHPUnit_Framework_TestCase { testMyMethod() { // Code here. } } 我的问题 我想知道测试套件中使用的命名是否有任何理由不能反映项目的源代码?例如,我认为文件名可以匹配: ├── src/ │ ├── classes/ │ │ ├── MyClass.php # Same │ └── ... ├── tests/ │ ├── testcases/ │ │ ├── MyClass.php # Same │ ├── bootstrap.php │ └── ... └── ... 如果使用PHP> 5.3,名称空间可用于允许类名匹配: namespace MyProjectMyTests; 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. } } 请注意,使用了@tests注释,因此方法名称可以匹配. 解决方法
有理由不这样做: >在同一名称空间中测试和测试类是有意义的 use MyProjectMyClass as MyActualClass;
如果您将testMyMethod视为替代方案,这可能听起来很吸引人,但这不是惯例.相反,您应该使用更多描述性测试方法名称,如testThatMyMethodReturnsTrueIfFooIsBar. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |