testmonitor/junit-xml-parser

The TestMonitor JUnit XML parser.

dev-main 2025-02-18 13:47 UTC

This package is auto-updated.

Last update: 2025-02-18 13:47:42 UTC


README

Latest Stable Version CircleCI StyleCI codecov License

This package provides a very basic, convenient parser for JUnit XML reports.

Table of Contents

Installation

To install the client you need to require the package using composer:

$ composer require testmonitor/junit-xml-parser

Use composer's autoload:

require __DIR__.'/../vendor/autoload.php';

You're all set up now!

Usage

Include the parser in your project:

use TestMonitor\JUnitXmlParser\JUnitXmlParser;

$parser = new JUnitXmlParser();
$testSuites = $parser->parse('path/to/junit.xml');

Examples

Below are some examples demonstrating how to use the JUnit XML Parser to extract and process test results.

Parsing a JUnit XML file

This example shows how to parse a JUnit XML report and retrieve test suite and test case information.

use TestMonitor\JUnitXmlParser\JUnitXmlParser;

$parser = new JUnitXmlParser();
$testSuites = $parser->parse('tests/results.xml');

foreach ($testSuites as $suite) {
    echo "Suite: " . $suite->getName() . "\n";

    foreach ($suite->getTestCases() as $testCase) {
        echo "  Test: " . $testCase->getName() . " - Status: " . $testCase->getStatus()->name . "\n";
    }
}

Processing Failures and Skipped Tests

This example demonstrates how to identify and handle failed and skipped test cases.

use TestMonitor\JUnitXmlParser\JUnitXmlParser;

$parser = new JUnitXmlParser();
$testSuites = $parser->parse('tests/results.xml');

foreach ($testSuites as $suite) {
    foreach ($suite->getTestCases() as $testCase) {
        if ($testCase->getStatus() === TestStatus::FAILED) {
            echo "Test " . $testCase->getName() . " failed: " . $testCase->getFailureMessage() . "\n";
        } elseif ($testCase->getStatus() === TestStatus::SKIPPED) {
            echo "Test " . $testCase->getName() . " was skipped.\n";
        }
    }
}

Extracting Execution Time and Timestamp

JUnit XML reports include execution times and timestamps, which can be accessed as shown below.

use TestMonitor\JUnitXmlParser\JUnitXmlParser;

$parser = new JUnitXmlParser();
$testSuites = $parser->parse('tests/results.xml');

foreach ($testSuites as $suite) {
    echo "Suite: " . $suite->getName() . " executed in " . $suite->getDuration() . " seconds on " . $suite->getTimestamp() . "\n";
}

Tests

The package contains integration tests. You can run them using PHPUnit.

$ vendor/bin/phpunit

Changelog

Refer to CHANGELOG for more information.

Contributing

Refer to CONTRIBUTING for contributing details.

Credits

License

The MIT License (MIT). Refer to the License for more information.