4
Help us understand the problem. What are the problem?

posted at

updated at

Organization

GitHub ActionsでLaravelのPHPUnitを並列実行

背景

  • 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 の並列実行

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Sign upLogin
4
Help us understand the problem. What are the problem?