今天來談 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
我們可以善用這些參數達成我們要的效果。
立基於 PHPUnit 之上,Pest 的語法比起原本的語法,又可以更加語意化
我們可以用 php artisan make:test
建立
test('example', function () {
$response = $this->get('/');
$response->assertStatus(200);
});
到了 Pest 4 我們甚至可以寫前端相關的測試
$routes = ['/', '/about', '/contact'];
visit($routes)->assertNoSmoke();
今天的部分就到這邊,我們明天見!