8
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

GitHub ActionsでLaravelのPHPUnitを並列実行

Last updated at Posted at 2021-04-26

背景

  • GitHub Actionsでのテストの数が多すぎるので分散処理がしたい
  • 良参考記事があったのでそれを元にLaravel用に設定
  • Laravelは5.5~6.xを想定

結論

.github/workflows/phpunit.yml
name: PHPUnit
on:
.
.
.
#略


jobs:
  test-php:
    name: Test PHP
    runs-on: ubuntu-latest
    strategy:
      fail-fast: false
      matrix:
        parallelism: [5]
        id: [0,1,2,3,4]
.
.
.
#略
    services:
      mysql:
        image: mysql:5.7
.
.
.
#略
    steps:
      - uses: actions/checkout@v2
      - name: Setup PHP
.
.
.
#略
      - name: Run Tests
        run: |
          find tests/ -name '*Test.php' | sort | awk "NR % ${{ matrix.parallelism }} == ${{ matrix.id }}" | xargs php ./create_multithread_phpunit_xml.php
          ./vendor/bin/phpunit --configuration ./ci_phpunit.xml
create_multithread_phpunit_xml.php
<?php

$baseDir = realpath('./');
$files   = array_slice($argv, 1);
$xmlFileStringData = [];
foreach ($files as $file) {
    $xmlFileStringData[] = "<file>{$baseDir}/{$file}</file>";
}
$testFileString = implode("\n", $xmlFileStringData);
$template = <<<XML
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
         backupStaticAttributes="false"
         bootstrap="vendor/autoload.php"
         colors="true"
         convertErrorsToExceptions="true"
         convertNoticesToExceptions="true"
         convertWarningsToExceptions="true"
         processIsolation="false"
         stopOnFailure="false">
        <testsuite name="Test Case">
            {$testFileString}
        </testsuite>
    <php>
        <env name="APP_ENV" value="testing"/>
        <env name="CACHE_DRIVER" value="array"/>
        <env name="SESSION_DRIVER" value="array"/>
        <env name="QUEUE_DRIVER" value="sync"/>
        <env name="DB_DATABASE" value="test_db"/>
        <env name="DB_USERNAME" value="root"/>
        <env name="DB_PASSWORD" value="root"/>
    </php>
</phpunit>
XML;

file_put_contents("ci_phpunit.xml", $template);

基本的にはGithubActions で phpunit の並列実行を参照いただけると。

結果

こうなる。

Image from Gyazo

参考記事

GithubActions で phpunit の並列実行

8
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?