Codeception 是一個 testing framework,除了 unit test 以外,還包含了 functional test、behavior test,並提供各種 module 供整合。
到專案目錄使用 composer 安裝 Codeception:
composer require codeception/codeception --dev
初始化 unit test 的設定:
php vendor/bin/codecept init Unit This will install Codeception for unit testing only ? Where tests will be stored? (tests) Codeception provides additional features for integration tests Like accessing frameworks, ORM, Database. ? Do you wish to enable them? (y/n) n Adding codeception/module-asserts for Asserts to composer.json 1 new packages added to require-dev ? composer.json updated. Do you want to run "composer update"? (y/n) y Running composer update
建立新的 unit test 可透過 generate:test
指令來建立測試範例:
php vendor/bin/codecept generate:test unit MyTest
產生的檔案會放在 tests/MyTest.php
。
樣板檔大概長這樣:
<?php namespace Tests\Unit; // 有使用才加,用不到可以刪除 use Tests\Support\ .... ; class ArrayQueueTest extends \Codeception\Test\Unit { protected function _before() { } // tests public function testSomeFeature() { } }
其中:
_before()
:相當於 PHPUnit 的setup()
。_after()
則是tearDown()
testSomeFeature()
:只要是 public function 且是test
開頭,則自動視為 test case
透過 run
可以執行測試:
php vendor/bin/codecept run Codeception PHP Testing Framework v5.0.10 https://helpukrainewin.org [Seed] 2070615206 Tests.unit Tests (1) ----------------------------------------------------------------------------------------------------------------------------------------- ✔ ArrayQueueTest: Some feature(0.00s) -------------------------------------------------------------------------------------------------------------------------------------------------------------- Time: 00:00.045, Memory: 10.00 MB