加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

PHPUnit测试套件命名约定

发布时间:2020-12-13 22:25:27 所属栏目:PHP教程 来源:网络整理
导读:PHPUnit manual强调了一些惯例: MyClass类的测试进入MyClassTest类 MyClassTest类存在于MyClassTest.php文件中 MyClassTest继承自PHPUnit_Framework_TestCase 测试是公共方法,名为test * 这将导致类似于此文件夹结构: ├── src/│ ├── classes/│ │
PHPUnit manual强调了一些惯例:

> MyClass类的测试进入MyClassTest类
> MyClassTest类存在于MyClassTest.php文件中
> MyClassTest继承自PHPUnit_Framework_TestCase
>测试是公共方法,名为test *

这将导致类似于此文件夹结构:

├── 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注释,因此方法名称可以匹配.

解决方法

And if using PHP > 5.3,namespaces can be used to allow class names to match:

有理由不这样做:

>在同一名称空间中测试和测试类是有意义的
>否则,您需要使用类别名导入受测试的类,以将其与测试用例区分开来:

use MyProjectMyClass as MyActualClass;

The method name MyMethod matches the method name used in my project’s source.

如果您将testMyMethod视为替代方案,这可能听起来很吸引人,但这不是惯例.相反,您应该使用更多描述性测试方法名称,如testThatMyMethodReturnsTrueIfFooIsBar.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读