iT邦幫忙

2025 iThome 鐵人賽

DAY 27
0
Modern Web

Laravel 12 開發者幸福度升級指南系列 第 27

Day 27:Laravel 的自動化測試與 Pest

  • 分享至 

  • xImage
  •  

今天來談 Laravel 在自動化測試上,怎麼改進開發者撰寫的體驗

自動化測試

不管專案是使用 phpunit 或者 pest,透過 php artisan test 可以很快的運作自動化測試。

並且接受許多的參數,我們可以透過 php artisan test --help 看。

php artisan test --help

Description:
  Run the application tests

Usage:
  test [options]

Options:
      --without-tty         Disable output to TTY
      --compact             Indicates whether the compact printer should be used
      --coverage            Indicates whether code coverage information should be collected
      --min[=MIN]           Indicates the minimum threshold enforcement for code coverage
  -p, --parallel            Indicates if the tests should run in parallel
      --profile             Lists top 10 slowest tests
      --recreate-databases  Indicates if the test databases should be re-created
      --drop-databases      Indicates if the test databases should be dropped
      --without-databases   Indicates if database configuration should be performed
  -h, --help                Display help for the given command. When no command is given display help for the list command
      --silent              Do not output any message
  -q, --quiet               Only errors are displayed. All other output is suppressed
  -V, --version             Display this application version
      --ansi|--no-ansi      Force (or disable --no-ansi) ANSI output
  -n, --no-interaction      Do not ask any interactive question
      --env[=ENV]           The environment the command should run under
  -v|vv|vvv, --verbose      Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug

我們可以善用這些參數達成我們要的效果。

Pest 的語法

立基於 PHPUnit 之上,Pest 的語法比起原本的語法,又可以更加語意化

我們可以用 php artisan make:test 建立

test('example', function () {
    $response = $this->get('/');

    $response->assertStatus(200);
});

到了 Pest 4 我們甚至可以寫前端相關的測試

$routes = ['/', '/about', '/contact'];
 
visit($routes)->assertNoSmoke();

今天的部分就到這邊,我們明天見!


上一篇
Day 26:Laravel 怎麼提升使用者在開發階段的體驗
系列文
Laravel 12 開發者幸福度升級指南27
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言