JunitXml

PHP library for generate XML document in JUnit format.

View project on GitHub

JunitXml

PHP CI/CD GitHub license PHP version

PHP library for generate XML document in JUnit format.

Installation

Requirements

JunitXml requires PHP 5.5.9 or above on your machine.

With Composer

With composer, you can add JunitXml as a dependency for a project with:

php composer.phar require llaumgui/junit-xml

Usage

  • Create a new TestSuites:
$testSuites = new JunitXmlTestSuites('My testsuites');
  • Add a TestSuite to the TestSuites:
$testSuite1 = $testSuites->addTestSuite('My testsuite #1');
  • Add a TestCase to the TestSuite:
$test1 = $testSuite1->addTest('Check if blabla is good');
  • Increment the assertion number of the TestCase:
$test1->incAssertions();
  • Mark test as skipped:
$test1->addSkipped('Skipped because blabla.');
  • Mark test in error:
$test1->addError('Error message', 'error_type');
  • Mark test as fail:
$test1->addFailure('Fail message', 'fail_type');
  • Finish and close test:
$test1->finish();
  • Finish and close TestSuite:
$testSuite->finish();
  • Finish, close and get XML of the TestSuites:
$testSuites->getXml();

Testing

JUnitXML provides the JunitXml\JunitXmlValidation class. This class provides statics methods for:

  • validateXsdFromString: Validate a XML string with the Junit v4 XSD.
  • getTestableXmlOutput: Normalize the variants: time and timestamp.

Example

// Test file output format
$this->assertTrue(JunitXmlValidation::validateXsdFromString(
    file_get_content('junit.xml')
));

// Test file output content
$this->assertXmlStringEqualsXmlString(
    file_get_content('good_junit.xml'),
    JunitXmlValidation::getTestableXmlOutput(file_get_content('junit.xml')
);