基本說明
建立測試檔案
要建立新的測試案例,請使用 make:test Artisan 指令。預設情況下,測試會放在 tests/Feature 目錄中
PS W:\xampp\htdocs\twingo> php artisan make:test UserTest
   INFO  Test [W:\xampp\htdocs\twingo\tests\Feature\UserTest.php] created successfully. 如果您想在 tests/Unit 目錄中建立測試,可以在執行 make:test 指令時使用 --unit 選項
PS W:\xampp\htdocs\twingo> php artisan make:test Auth\Users --unit
   INFO  Test [W:\xampp\htdocs\twingo\tests\Unit\Auth\Users.php] created successfully. 建立好的測試檔案Users.php內容如下
<?php
namespace Tests\Unit\Auth;
use PHPUnit\Framework\TestCase;
class Users extends TestCase
{
    /**
     * A basic unit test example.
     */
    public function test_example(): void
    {
        echo 'Users Test'; // 增加這行觀看輸出結果
        $this->assertTrue(true);
    }
}
執行測試
Laravel 預設使用 phpunit 來作測試工具
./vendor/bin/phpunit
// 上述指令等同下行指令
php artisan test若是指定在 tests/Unit/Auth 目錄下的測試檔 Users.php,則指令如下
PS W:\xampp\htdocs\twingo> ./vendor/bin/phpunit tests/Unit/Auth/Users.php
// 或
PS W:\xampp\htdocs\twingo> php artisan test  tests/Unit/Auth/Users.php平行執行測試
預設情況下,Laravel 和 Pest / PHPUnit 會在單一程序中依序執行你的測試。但是也可以在多個程序中同時執行測試,大幅縮短執行測試所需的時間。首先,你應安裝 brianium/paratest Composer 套件作為「開發」相依項。然後,在執行 test Artisan 指令時包含 --parallel 選項
安裝 brianium/paratest 套件
composer require brianium/paratest --dev執行的結果
PS W:\xampp\htdocs\twingo> php artisan test --parallel --processes=4
ParaTest v7.4.5 upon PHPUnit 11.1.3 by Sebastian Bergmann and contributors.
Processes:     4
Runtime:       PHP 8.2.12
Configuration: W:\xampp\htdocs\twingo\phpunit.xml
..                                                                 2 / 2 (100%)
Time: 00:02.646, Memory: 24.00 MB
OK (2 tests, 2 assertions)指定檔案時,需參數放在前面,測試檔名放在後面
 
 
沒有留言:
張貼留言