yii2 - 關於PHP的單元測試,好難~~

來源:互聯網
上載者:User
關鍵字 php yii2 codeception
一直沒做過單元測試,對單元測試、功能測試的區別還是不理解

兩個問題:

  • 有沒有開源的程式,帶單元測試的例子,推薦個。最好是codecept單元測試架構的程式。 希望通過閱讀代碼來加深對單元測試的理解。

  • 說說我目前對單元測試的認識,歡迎指正。

我目前的項目中 controller->service->repository->entity
目前我只對service層做單元測試

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));        });    }

上面是我寫的最基本的例子,測試ProductService類的測試類別

我測試了儲存商品的方法,在_before時,為ProductRepository打樁,ProductRepository::save()直接返回ProductEntity執行個體。

感覺怪怪的,這樣的單元測試,只是測試這個方法能不能跑起來,對裡面的邏輯因為打樁的關係,都略過了。

如果要測試具體的邏輯,比如能不能儲存到資料庫中,就需要entity做測試,需要對資料庫做操作,那這樣,還是單元測試嗎?我感覺變成的功能測試了。

另外,能通過注入repository屬性的方法打樁,是因為ProductService中用了依賴注入的方法,如果
ProductService::save()方法中不是用了依賴注入ProductRepository,而是內部的一個service,就不能通過屬性注入的方式打樁,這種情況要怎麼處理?

ProductService類public function save($data){    if(empty($data)){        //這裡的AnotherService如何打樁?        $service = Yii::createObject(AnotherService::className());        $data = $service->getData();    }      return $this->repository->save($data);}

回複內容:

一直沒做過單元測試,對單元測試、功能測試的區別還是不理解

兩個問題:

  • 有沒有開源的程式,帶單元測試的例子,推薦個。最好是codecept單元測試架構的程式。 希望通過閱讀代碼來加深對單元測試的理解。

  • 說說我目前對單元測試的認識,歡迎指正。

我目前的項目中 controller->service->repository->entity
目前我只對service層做單元測試

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));        });    }

上面是我寫的最基本的例子,測試ProductService類的測試類別

我測試了儲存商品的方法,在_before時,為ProductRepository打樁,ProductRepository::save()直接返回ProductEntity執行個體。

感覺怪怪的,這樣的單元測試,只是測試這個方法能不能跑起來,對裡面的邏輯因為打樁的關係,都略過了。

如果要測試具體的邏輯,比如能不能儲存到資料庫中,就需要entity做測試,需要對資料庫做操作,那這樣,還是單元測試嗎?我感覺變成的功能測試了。

另外,能通過注入repository屬性的方法打樁,是因為ProductService中用了依賴注入的方法,如果
ProductService::save()方法中不是用了依賴注入ProductRepository,而是內部的一個service,就不能通過屬性注入的方式打樁,這種情況要怎麼處理?

ProductService類public function save($data){    if(empty($data)){        //這裡的AnotherService如何打樁?        $service = Yii::createObject(AnotherService::className());        $data = $service->getData();    }      return $this->repository->save($data);}

可以臨時修改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
內容下面有下載連結

你邀請了誰?

  • 相關文章

    聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.