天天看點

ThinkPHP6.0使用PHPUnit進行單元測試

ThinkPHP6.0使用PHPUnit進行單元測試

安裝

composer require --dev phpunit/phpunit      

示例

<?php
declare(strict_types=1);

use PHPUnit\Framework\TestCase;

class ServiceTest extends TestCase
{
    /**
     * @doesNotPerformAssertions
     */
    public function testHello(){
        echo 'hello';
    }
}      

PHPUnit配合PhpStorm使用,可以直接執行單個測試函數

ThinkPHP6.0使用PHPUnit進行單元測試

測試Model

如果需要測試資料庫Model,需要在測試檔案頂部,對ThinkPHP應用初始化

<?php
declare(strict_types=1);

// ### 需要手動初始化 
require_once __DIR__ . '/../../vendor/autoload.php';

((new \think\App())->http)->run();
// ### 

use PHPUnit\Framework\TestCase;

      

參考

tp6.0如何進行單元測試 研究thinkphp6的phpunit整合血淚史