testmonitor / junit-xml-parser
The TestMonitor JUnit XML parser.
Requires
- php: ^8.3
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.1
- phpunit/phpunit: ^11.0
- squizlabs/php_codesniffer: ^3.7
This package is auto-updated.
Last update: 2025-02-18 13:47:42 UTC
README
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
- Thijs Kok - Lead developer - ThijsKok
- Stephan Grootveld - Developer - Stefanius
- Frank Keulen - Developer - FrankIsGek
License
The MIT License (MIT). Refer to the License for more information.