SimpleTest(v1.1.7)でJUnit XMLを出力するには

JenkinsのTest Result Trendのグラフに表示するために、SimpleTest v1.1.7のテスト結果をJUnit XMLで出力するには、どうすればいいのでしょうか?

SimpleTestとは

SimpleTest - Unit Testing for PHP
GitHub - simpletest/simpletest: SimpleTest - Unit Testing for PHP
SimpleTest - Unit Testing for PHP. Contribute to simpletest/simpletest development by creating an account on GitHub.

PHPのxUnitTestフレームワークの一つです。CakePHP 1.2、1.3の標準テストフレームワークでした。

SimpleTest v1.2.0

-jオプションを指定すると、JUnit XMLで表示します。

$ php AllTests.php -j
<?xml version="1.0"?>
<!-- starting test suite SmokeTest.php
- case start SmokeTest
  - test start: test_1
-->

<testsuite name="SmokeTest.php" timestamp="2020-11-03T16:00:53+09:00" hostname="localhost" tests="1" failures="0" errors="0" time="0.0055310726165771">
  <testcase name="test_1" classname="SmokeTest" time="0.00023388862609863"/>
</testsuite>Code language: plaintext (plaintext)

SimpleTest v1.1.7

JenkinsのTest Result Trendのグラフを表示したい

JenkinsのTest Result Trendのグラフは、テストを作るはげみです。

SimpleTestの実行結果は、次のようなサマリーです。

$ php AllTests.php
AllTests.php
OK
Test cases run: 1/1, Passes: 1, Failures: 0, Exceptions: 0
Code language: plaintext (plaintext)

このサマリー形式では、Jenkinsのグラフに反映できません。反映するためには、テスト結果をJUnit XMLで出力する必要があります。

すばらしいことに、SimpleTestには、JUnitXMLReporterがついてきます。TestSuiteクラスやUnitTestCaseクラスのrunメソッドにJUnitXMLReporterのインスタンスを渡すと、テスト結果をJUnit XMLで表示します。

ところが、思ったとおりのJUnit XMLを得るまで、なかなか大変でした。

最初の状態

次のようなAllTestsとMyTestがあるとします。AllTestsはTestSuite、MyTestはUnitTestCaseです。

<?php

require_once(__DIR__ . '/../vendor/simpletest/simpletest/autorun.php');

class AllTests extends TestSuite
{
    public function __construct()
    {
        parent::__construct('All tests');

        $arr = glob(__DIR__ . '/*Test.php');
        foreach ($arr as $test_file) {
            if (__FILE__ == $test_file) {
                continue;
            }

            $this->addFile($test_file);
        }
    }
}
Code language: PHP (php)
<?php

require_once(__DIR__ . '/../vendor/simpletest/simpletest/autorun.php');

class SmokeTest extends UnitTestCase
{
    public function test_1()
    {
        $this->assertEqual(1, 1);
    }
}
Code language: PHP (php)

ターミナルで実行すると、テスト結果のサマリーが表示されます。AllTests.php、SmokeTest.phpどちらも単独で実行できます。

$ php AllTests.php
AllTests.php
OK
Test cases run: 1/1, Passes: 1, Failures: 0, Exceptions: 0

$ php SmokeTest.php 
SmokeTest.php
OK
Test cases run: 1/1, Passes: 1, Failures: 0, Exceptions: 0
Code language: plaintext (plaintext)

実験コード1

まず、junit_xml_reporter.phpをrequire_onceして、runメソッドにJUnitXMLReporterインスタンスを渡しました。

<?php

require_once(__DIR__ . '/../vendor/simpletest/simpletest/autorun.php');
require_once(__DIR__ . '/../vendor/simpletest/simpletest/extensions/junit_xml_reporter.php');

class AllTests extends TestSuite
{
    public function __construct()
    {
        parent::__construct('All tests');

        $arr = glob(__DIR__ . '/*Test.php');
        foreach ($arr as $test_file) {
            if (__FILE__ == $test_file) {
                continue;
            }

            $this->addFile($test_file);
        }
    }
}

$suite = new AllTests();
$suite->run(new JUnitXMLReporter());
Code language: PHP (php)

実行すると、JUnit XMLが表示されました。

しかし続けてサマリーが表示されてしまいました。サマリーの内容も変です。テストケースは1個しかないのに、テストケースが2個、合格が2個とあります。

$ php AllTests.php
<?xml version="1.0"?>
<!-- starting test suite All tests
- case start SmokeTest
  - test start: test_1
-->

<testsuite name="All tests" timestamp="2020-11-03T15:06:19+09:00" hostname="localhost" tests="1" failures="0" errors="0" time="0.0085961818695068">
  <testcase name="test_1" classname="SmokeTest" time="0.00030899047851562"/>
</testsuite>

AllTests.php
OK
Test cases run: 2/2, Passes: 2, Failures: 0, Exceptions: 0

Code language: plaintext (plaintext)

AllTests内の$this->run(new JUnitXMLReporter()); で、1回。

simpletest/simpletest.phpのsimpletest_autorun()で、1回。

合わせて2回、テストが走ってしまいました。

実験コード2

AllTests.phpは、最初の状態に戻しました。

simpletest_autorun()が必ず走る設定になっています。この箇所のReporterは、DefaultReporterクラスです。

function run_local_tests()
{
    try {
        if (tests_have_run()) {
            return;
        }
        $candidates = capture_new_classes();
        $loader = new SimpleFileLoader();
        $suite = $loader->createSuiteFromClasses(
                basename(initial_file()),
                $loader->selectRunnableTests($candidates));
        return $suite->run(new DefaultReporter());
    } catch (Exception $stack_frame_fix) {
        print $stack_frame_fix->getMessage();
        return false;
    }
}

Code language: PHP (php)

この箇所のDefaultReporterをJUnitXMLReportにしてみました。

require_once __DIR__ . '/extensions/junit_xml_reporter.php'; // ★これを追加

省略

function run_local_tests()
{
    try {
        if (tests_have_run()) {
            return;
        }
        $candidates = capture_new_classes();
        $loader = new SimpleFileLoader();
        $suite = $loader->createSuiteFromClasses(
                basename(initial_file()),
                $loader->selectRunnableTests($candidates));
        return $suite->run(new JUnitXMLReporter());  // ★ここを修正
    } catch (Exception $stack_frame_fix) {
        print $stack_frame_fix->getMessage();
        return false;
    }
}

Code language: PHP (php)

今度は、JUnit XMLだけが表示されました。

$ php AllTests.php
<?xml version="1.0"?>
<!-- starting test suite AllTests.php
- case start SmokeTest
  - test start: test_1
-->

<testsuite name="AllTests.php" timestamp="2020-11-03T15:38:59+09:00" hostname="localhost" tests="1" failures="0" errors="0" time="0.0066690444946289">
  <testcase name="test_1" classname="SmokeTest" time="0.00027298927307129"/>
</testsuite>

Code language: plaintext (plaintext)

composerが管理しているvendor下のファイルなので、元に戻しておきます。

DefaultReporterは、Webアクセス時は、HtmlReporter、ターミナル起動時は、-xオプションでXmlReporter、そうでなければSimpleReporterという振り分けをしていました。

このReporterの振り分け処理をカスタマイズするメソッドがあるといいのですが、ぼくの調べたところでは見つかりませんでした。

最終的なコード

simpletest/simpletest/autorun.php をプロジェクトのtestディレクトリなどにコピーして、my_autorun.php にします。AllTests.php や SmokeTest.php からは、この my_autorun.php を require_onceします。

<?php

require_once(__DIR__ . '/my_autorun.php');

class AllTests extends TestSuite
{
    public function __construct()
    {
        parent::__construct('All tests');

        $arr = glob(__DIR__ . '/*Test.php');
        foreach ($arr as $test_file) {
            if (__FILE__ == $test_file) {
                continue;
            }

            $this->addFile($test_file);
        }
    }
}
Code language: PHP (php)
<?php

require_once(__DIR__ . '/my_autorun.php');

class SmokeTest extends UnitTestCase
{
    public function test_1()
    {
        $this->assertEqual(1, 1);
    }
}
Code language: PHP (php)

my_autorun.phpを3箇所編集します。

(1)コピーした位置に合わせて、require_onceのパスを修正します。arguments.php と extensions/junit_xml_reporter.php のreuqire_onceを追加します。

$simpletestDir = __DIR__ . '/../vendor/simpletest/simpletest';
require_once $simpletestDir . '/unit_tester.php';
require_once $simpletestDir . '/mock_objects.php';
require_once $simpletestDir . '/collector.php';
require_once $simpletestDir . '/default_reporter.php';
require_once $simpletestDir . '/arguments.php';
require_once $simpletestDir . '/extensions/junit_xml_reporter.php';
Code language: PHP (php)

(2)55行目、runメソッドに渡すReporterクラスです。

// 修正前
        return $suite->run(new JUnitXMLReporter());Code language: PHP (php)
// 修正後
        $reporter = reporter_factory();
        return $suite->run($reporter);
Code language: PHP (php)

(3)ファイルの最後に、reporter_factory関数を追加します。

/**
 * DefaultReporter
 *      $ php MyTest.php
 *
 * JUnitXMLReporter
 *      $ php MyTest.php --junit
 *
 * @return DefaultReporter|JUnitXMLReporter
 */
function reporter_factory()
{
    $simpleArguments = new SimpleArguments($_SERVER['argv']);
    $vars = $simpleArguments->all();
    if (isset($vars['junit'])) {
        return new JUnitXMLReporter();
    } else {
        return new DefaultReporter();
    }
}
Code language: PHP (php)

my_autorun.phpの全て(simpletest v1.1.7ベース)

<?php
/**
 *  Autorunner which runs all tests cases found in a file
 *  that includes this module.
 *  @package    SimpleTest
 */

/**#@+
 * include simpletest files
 */
$simpletestDir = __DIR__ . '/../vendor/simpletest/simpletest';
require_once $simpletestDir . '/unit_tester.php';
require_once $simpletestDir . '/mock_objects.php';
require_once $simpletestDir . '/collector.php';
require_once $simpletestDir . '/default_reporter.php';
require_once $simpletestDir . '/arguments.php';
require_once $simpletestDir . '/extensions/junit_xml_reporter.php';

/**#@-*/

$GLOBALS['SIMPLETEST_AUTORUNNER_INITIAL_CLASSES'] = get_declared_classes();
$GLOBALS['SIMPLETEST_AUTORUNNER_INITIAL_PATH'] = getcwd();
register_shutdown_function('simpletest_autorun');

/**
 *    Exit handler to run all recent test cases and exit system if in CLI
 */
function simpletest_autorun()
{
    chdir($GLOBALS['SIMPLETEST_AUTORUNNER_INITIAL_PATH']);
    if (tests_have_run()) {
        return;
    }
    $result = run_local_tests();
    if (SimpleReporter::inCli()) {
        exit($result ? 0 : 1);
    }
}

/**
 *    run all recent test cases if no test has
 *    so far been run. Uses the DefaultReporter which can have
 *    it's output controlled with SimpleTest::prefer().
 *    @return boolean/null false if there were test failures, true if
 *                         there were no failures, null if tests are
 *                         already running
 */
function run_local_tests()
{
    try {
        if (tests_have_run()) {
            return;
        }
        $candidates = capture_new_classes();
        $loader = new SimpleFileLoader();
        $suite = $loader->createSuiteFromClasses(
            basename(initial_file()),
            $loader->selectRunnableTests($candidates));
        $reporter = reporter_factory();
        return $suite->run($reporter);
    } catch (Exception $stack_frame_fix) {
        print $stack_frame_fix->getMessage();
        return false;
    }
}

/**
 *    Checks the current test context to see if a test has
 *    ever been run.
 *    @return boolean        True if tests have run.
 */
function tests_have_run()
{
    $context = SimpleTest::getContext();
    if ($context) {
        return (boolean)$context->getTest();
    }
    return false;
}

/**
 *    The first autorun file.
 *    @return string        Filename of first autorun script.
 */
function initial_file()
{
    static $file = false;
    if (! $file) {
        if (isset($_SERVER, $_SERVER['SCRIPT_FILENAME'])) {
            $file = $_SERVER['SCRIPT_FILENAME'];
        } else {
            $included_files = get_included_files();
            $file = reset($included_files);
        }
    }
    return $file;
}

/**
 *    Every class since the first autorun include. This
 *    is safe enough if require_once() is always used.
 *    @return array        Class names.
 */
function capture_new_classes()
{
    global $SIMPLETEST_AUTORUNNER_INITIAL_CLASSES;
    return array_map('strtolower', array_diff(get_declared_classes(),
        $SIMPLETEST_AUTORUNNER_INITIAL_CLASSES ?
            $SIMPLETEST_AUTORUNNER_INITIAL_CLASSES : array()));
}

/**
 * DefaultReporter
 *      $ php MyTest.php
 *
 * JUnitXMLReporter
 *      $ php MyTest.php --junit
 *
 * @return DefaultReporter|JUnitXMLReporter
 */
function reporter_factory()
{
    $simpleArguments = new SimpleArguments($_SERVER['argv']);
    $vars = $simpleArguments->all();
    if (isset($vars['junit'])) {
        return new JUnitXMLReporter();
    } else {
        return new DefaultReporter();
    }
}

Code language: PHP (php)

起動方法

オプションなし、サマリー表示。

$ php AllTests.php
AllTests.php
OK
Test cases run: 1/1, Passes: 1, Failures: 0, Exceptions: 0
Code language: Bash (bash)

-xオプションは、XML表示。simpletestの標準機能です。

$ php AllTests.php -x
<?xml version="1.0"?>
<run>
  <group size="1">
    <name>AllTests.php</name>
    <group size="1">
      <name>All tests</name>
      <group size="1">
        <name>/var/www/html/tests/SmokeTest.php</name>
        <case>
          <name>SmokeTest</name>
          <test>
            <name>test_1</name>
            <pass>Equal expectation [Integer: 1] at [/var/www/html/tests/SmokeTest.php line 12]</pass>
          </test>
        </case>
      </group>
    </group>
  </group>
</run>
Code language: Bash (bash)

--junitオプションは、JUnit XML表示。今回追加した機能です。

$ php AllTests.php --junit
<?xml version="1.0"?>
<!-- starting test suite AllTests.php
- case start SmokeTest
  - test start: test_1
-->

<testsuite name="AllTests.php" timestamp="2020-11-03T15:56:15+09:00" hostname="localhost" tests="1" failures="0" errors="0" time="0.004612922668457">
  <testcase name="test_1" classname="SmokeTest" time="0.00020813941955566"/>
</testsuite>

Code language: Bash (bash)

TestSuiteだけでなく、UnitTestCaseも実行できます。

オプションなし、サマリー表示

$ php SmokeTest.php 
SmokeTest.php
OK
Test cases run: 1/1, Passes: 1, Failures: 0, Exceptions: 0

Code language: Bash (bash)

--junitオプション、JUnit XML表示。

$ php SmokeTest.php --junit
<?xml version="1.0"?>
<!-- starting test suite SmokeTest.php
- case start SmokeTest
  - test start: test_1
-->

<testsuite name="SmokeTest.php" timestamp="2020-11-03T16:00:53+09:00" hostname="localhost" tests="1" failures="0" errors="0" time="0.0055310726165771">
  <testcase name="test_1" classname="SmokeTest" time="0.00023388862609863"/>
</testsuite>

Code language: Bash (bash)

JenkinsのTest Result Trendのグラフを作るには

JenkinsのTest Result Trendのグラフを作るには、
JobのConfigureページ

Post-build Actionsセクション

[Add post-build action]ボタンをクリック

メニューから[Publish JUnit test result report]を選択します。

[Publish JUnit test result report]は、simpletetestのJUnit XMLをそのまま処理できました。

メニューに、名前がそっくりな[Publish xUnit test result report]もあるんだけど?

[Add post-build action]メニューをよく見ると、名前がそっくりな[Publish xUnit test result report]もあります。こちらもJenkinsのTest Result Trendのグラフを作ります。

ところが、[Publish xUnit test result report]では、simpletetestのJUnit XMLはフォーマットエラーで読み込めませんでした。コメントを削除したり、time属性の小数点4桁以下を削除することで、読み込むことができました。

JUnitを使おうかな

参考記事

Obtaining JUnit-compatible XML from SimpleTest | luhman.org

この記事では、tidy_repair_string関数を使って「$1文字 + JUnit XML + サマリー」を整形していました。

この記事のおかげで、JUnitXMLReporterがついているのを知ったのよ

タイトルとURLをコピーしました