yii2 - 关于PHP的单元测试,好难~~

WBOY
Release: 2016-06-06 20:08:16
Original
1137 people have browsed it

一直没做过单元测试,对单元测试、功能测试的区别还是不理解

两个问题:

  • 有没有开源的程序,带单元测试的例子,推荐个。最好是codecept单元测试框架的程序。 希望通过阅读代码来加深对单元测试的理解。

  • 说说我目前对单元测试的认识,欢迎指正。

我目前的项目中 controller->service->repository->entity
目前我只对service层做单元测试

<code class="php">class ProductServiceTest extends \Codeception\TestCase\Test
{
    use Specify;

    protected $tester;
    private $_service;

    protected function _before()
    {
        $this->_service = Yii::createObject(ProductService::className());

        //mock repository
        $this->_service->repository = Stub::make(ProductRepository::className(), [
            'save' => function () {
                return new ProductEntity();
            },
        ]);
    }

    protected function _after()
    {
    }

    public function testSave()
    {
        $this->specify("保存产品", function () {  
            $data = [];         
            $this->assertInstanceOf(
                ProductEntity::className(),
                $this->_service->save($data));
        });
    }
</code>
Copy after login
Copy after login

上面是我写的最基本的例子,测试ProductService类的测试类

我测试了保存商品的方法,在_before时,为ProductRepository打桩,ProductRepository::save()直接返回ProductEntity实例。

感觉怪怪的,这样的单元测试,只是测试这个方法能不能跑起来,对里面的逻辑因为打桩的关系,都略过了。

如果要测试具体的逻辑,比如能不能保存到数据库中,就需要entity做测试,需要对数据库做操作,那这样,还是单元测试么?我感觉变成的功能测试了。

另外,能通过注入repository属性的方法打桩,是因为ProductService中用了依赖注入的方法,如果
ProductService::save()方法中不是用了依赖注入ProductRepository,而是内部的一个service,就不能通过属性注入的方式打桩,这种情况要怎么处理?

<code class="php">ProductService类
public function save($data){
    if(empty($data)){
        //这里的AnotherService如何打桩?
        $service = Yii::createObject(AnotherService::className());
        $data = $service->getData();
    }
    
  return $this->repository->save($data);
}
</code>
Copy after login
Copy after login

回复内容:

一直没做过单元测试,对单元测试、功能测试的区别还是不理解

两个问题:

  • 有没有开源的程序,带单元测试的例子,推荐个。最好是codecept单元测试框架的程序。 希望通过阅读代码来加深对单元测试的理解。

  • 说说我目前对单元测试的认识,欢迎指正。

我目前的项目中 controller->service->repository->entity
目前我只对service层做单元测试

<code class="php">class ProductServiceTest extends \Codeception\TestCase\Test
{
    use Specify;

    protected $tester;
    private $_service;

    protected function _before()
    {
        $this->_service = Yii::createObject(ProductService::className());

        //mock repository
        $this->_service->repository = Stub::make(ProductRepository::className(), [
            'save' => function () {
                return new ProductEntity();
            },
        ]);
    }

    protected function _after()
    {
    }

    public function testSave()
    {
        $this->specify("保存产品", function () {  
            $data = [];         
            $this->assertInstanceOf(
                ProductEntity::className(),
                $this->_service->save($data));
        });
    }
</code>
Copy after login
Copy after login

上面是我写的最基本的例子,测试ProductService类的测试类

我测试了保存商品的方法,在_before时,为ProductRepository打桩,ProductRepository::save()直接返回ProductEntity实例。

感觉怪怪的,这样的单元测试,只是测试这个方法能不能跑起来,对里面的逻辑因为打桩的关系,都略过了。

如果要测试具体的逻辑,比如能不能保存到数据库中,就需要entity做测试,需要对数据库做操作,那这样,还是单元测试么?我感觉变成的功能测试了。

另外,能通过注入repository属性的方法打桩,是因为ProductService中用了依赖注入的方法,如果
ProductService::save()方法中不是用了依赖注入ProductRepository,而是内部的一个service,就不能通过属性注入的方式打桩,这种情况要怎么处理?

<code class="php">ProductService类
public function save($data){
    if(empty($data)){
        //这里的AnotherService如何打桩?
        $service = Yii::createObject(AnotherService::className());
        $data = $service->getData();
    }
    
  return $this->repository->save($data);
}
</code>
Copy after login
Copy after login

可以临时修改Yii::$classMap重定向AnotherService这个类到你的模拟类中,或者重写一个依赖注入的逻辑

我认为单元测试就是针对单元做测试,针对一系列做测试确实是功能测试

但怎样才算一系列呢,如果说mysql_query这个函数算一个单元的话,那这个函数的底层不也是执行了一系列的步骤吗?那这算不算一个功能?

所以如何界定单元和功能应该是要好好界定一下的,我的界定方法就是将接近PHP原生的对象方法理解为单元,没有过多层次的调用封装的

开发者自己将业务串联起来一起测试的这就是功能测试

你的单元测试代码基本是没错的,只是当这个测试用例有多个测试方法时,before方法会被多次调用,解决方案请见
http://www.kkh86.com/it/codeception/guide-unit-test-before-after.html

我有个简单的demo,还没空完善,请见
http://www.kkh86.com/it/codeception/guide-unit-test-apply.html
内容下面有下载链接

你邀请了谁?

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!