Skip to content

Zeroplex 生活隨筆

軟體開發、伺服器和生活瑣事

小 縮小字型大小。 中 重設字型大小。 大 放大字型大小。

安裝 Codeception 撰寫 unit test

Posted on 2023 年 4 月 20 日 By 日落 在〈安裝 Codeception 撰寫 unit test〉中尚無留言

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
Tags:Codeception, PHP

文章導覽

Previous Post: whois 查詢 domain record
Next Post: 緋染天空 Heaven Burns Red 地圖筆記

發佈留言 取消回覆

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *


其他

關於我  (About me)

小額贊助

  文章 RSS Feed

  留言 RSS Feed

Apache AWS Bash C/C++ Docker FreeBSD GCP Git Google Java JavaScript Laravel Linux Microsoft MSSQL MySQL Nginx PHP PHPUnit PostgreSQL Python Qt Ubuntu Unix Vim Web Windows WordPress XD 作業系統 分享 好站推薦 專題 攝影 新奇搞笑 新聞 旅遊 生活雜記 程式設計 網路架站 網頁設計 資訊學習 資訊安全 遊戲 音樂


創用 CC 授權條款
本著作係採用創用 CC 姓名標示-相同方式分享 4.0 國際 授權條款授權.