Yii - 測試



當我們編寫 PHP 類時,我們會逐步除錯它,或者使用 die 或 echo 語句來驗證它的工作方式。如果我們開發一個 web 應用,我們會輸入測試資料到表單中以確保頁面按預期工作。這個測試過程可以自動化。

自動測試方法對於長期專案很有意義,這些專案:

  • 複雜且龐大
  • 持續增長
  • 失敗成本過高

如果您的專案不會變得複雜,並且相對簡單,或者只是一個一次性專案,那麼自動化測試就顯得過分了。

準備測試

步驟 1 - 安裝 Codeception 框架。執行以下程式碼。

composer global require "codeception/codeception = 2.0.*"
composer global require "codeception/specify = *"
composer global require "codeception/verify = *"

步驟 2 - 執行以下命令。

composer global status

輸出為“Changed current directory to <directory>”。您應該將 '<directory>/vendor/bin' 新增到您的 PATH 變數中。在這種情況下,執行以下程式碼:

export PATH = $PATH:~/.composer/vendor/bin

步驟 3 - 建立一個名為 'yii2_basic_tests' 的新資料庫。

步驟 4 - 在 tests 目錄中執行。

codeception/bin/yii migrate

資料庫配置可以在 tests/codeception/config/config.php 中找到。

步驟 5 - 透過以下命令構建測試套件。

codecept build

Fixture

Fixture 的主要目的是在未知狀態下設定環境,以便您的測試以預期的方式執行。Yii 提供了一個近乎完整的 Fixture 框架。Yii Fixture 框架的一個關鍵概念是 Fixture 物件。它代表測試環境的特定方面。Fixture 物件是 yii\test\Fixture 類 的一個例項。

要定義一個 Fixture,您應該建立一個新類並從 yii\test\Fixture 或 yii\test\ActiveFixture 擴充套件它。前者更適合於通用 Fixture,而後者專門設計用於與資料庫和 ActiveRecord 一起工作。

單元測試

單元測試幫助您測試單個函式。例如,模型函式或元件類。

步驟 1 - 在 tests/codeception/fixtures 目錄下建立一個名為 ExampleFixture.php 的新 Fixture 檔案。

<?php
   namespace app\tests\codeception\fixtures;
   use yii\test\ActiveFixture;
   class ExampleFixture extends ActiveFixture {
      public $modelClass = ‘app⊨’MyUser';
   }
?>

步驟 2 - 然後,在 tests/codeception/unit/models 資料夾中建立一個名為 ExampleTest.php 的新測試檔案。

<?php
   namespace tests\codeception\unit\models;
   use app\models\MyUser;
   use yii\codeception\TestCase;
   class ExampleTest extends TestCase {
      public function testCreateMyUser() {
         $m = new MyUser();
         $m->name = "myuser";
         $m->email = "myser@email.com";
         $this->assertTrue($m->save());
      }
      public function testUpdateMyUser() {
         $m = new MyUser();
         $m->name = "myuser2";
         $m->email = "myser2@email.com";
         $this->assertTrue($m->save());
         $this->assertEquals("myuser2", $m->name);
      }
      public function testDeleteMyUser() {
         $m = MyUser::findOne(['name' => 'myuser2']);
         $this->assertNotNull($m);
         MyUser::deleteAll(['name' => $m->name]);
         $m = MyUser::findOne(['name' => 'myuser2']);
         $this->assertNull($m);
      }
   }
?>

在上面的程式碼中,我們定義了三個測試:

  • testCreateMyUser,
  • testUpdateMyUser,和
  • testDeleteMyUser。

我們剛剛建立了一個新使用者,更新了他的名字,並嘗試刪除他。我們根據 yii2_basic_tests 資料庫管理 MyUser 模型,這是一個我們真實資料庫的完整副本。

步驟 3 - 要啟動測試,移動到 tests 資料夾並執行。

codecept run unit models/ExampleTest

它應該透過所有測試。您將看到以下內容:

Unit Tests

功能測試

功能測試幫助您:

  • 使用瀏覽器模擬器測試應用程式
  • 驗證函式是否正常工作
  • 與資料庫互動
  • 將資料提交到伺服器端指令碼

在 tests 資料夾內執行:

generate:cept functional AboutPageCept

上面的命令會在 tests/codeception/functional 資料夾下建立 AboutPageCept.php 檔案。在這個功能測試中,我們將檢查我們的 about 頁面是否存在。

步驟 1 - 修改 AboutPageCept.php 檔案。

<?php
   $I = new FunctionalTester($scenario);
   $I->wantTo('perform actions and see result');
   $I->amOnPage('site/about');
   $I->see('about');
   $I->dontSee('apple');
?>

在上面給出的程式碼中,我們檢查了我們是否在 about 頁面上。顯然,我們應該在頁面上看到“about”這個詞,而不會看到“apple”。

步驟 2 - 透過以下命令執行測試。

run functional AboutPageCept

您將看到以下輸出:

Run Unit Tests
廣告