やりたいこと
Laravel11を使用したプロジェクトにて、main宛のPR作成時にGithub Actionsを使ったCIワークフローにてPHPUnitによる自動テストを実行したい
状況
一部のテストクラスのみ完成したため、まずはそのテストクラスだけをCI環境で自動テストとして動かしたい。そこで、php artisan testコマンドに存在するfilterオプションを使って、テスト実行対象クラスを限定させる(下記参照)。そしていざ実行
ci_sample.yaml
name: phpunit-tests
on:
pull_request:
branches: [main]
jobs:
test-company:
runs-on: ubuntu-latest
steps:
# 省略
- name: Run tests
run: |
php artisan test --filter "AuthenticationTest|EmailVerificationTest"
起こったこと
テストは全て成功しているのに、なぜかエラーが発生し失敗として扱われる
Run tests
PASS Tests\Feature\Auth\AuthenticationTest
✓ login screen can be rendered 0.37s
✓ users can authenticate using the login screen 0.07s
✓ users can not authenticate with invalid password 0.25s
✓ users can logout 0.03s
PASS Tests\Feature\Auth\EmailVerificationTest
✓ email verification screen can be rendered 0.04s
✓ email can be verified 0.03s
✓ email is not verified with invalid hash 0.03s
Tests: 7 passed (14 assertions)
Duration: 0.87s
Error: Process completed with exit code 1.
原因
テストメソッド未定義のテストクラスが存在していた。
下記テストクラスを削除したらCIワークフローが成功した。
tests/Feature/HogeControllerTest.php
<?php
namespace Tests\Feature;
use Tests\TestCase;
use Illuminate\Foundation\Testing\RefreshDatabase;
class HogeControllerTest extends TestCase
{
use RefreshDatabase;
//
}
まとめ
php artisan testのfilterでテスト実行対象クラスを限定させていても、対象外のテストクラスにメソッドを持たないものがあると、CIワークフローは失敗してしまうみたい。
ちなみに、テストに失敗する「filterで指定されていない実行対象外のテストクラス」があることは問題ない(filterによって実行までは移らないため)
そのため、とにかく「テストメソッドが未定義のテストクラス」が存在する状況は避けた方が良い。