Skip to content

ergebnis/phpunit-agent-reporter

phpunit-agent-reporter

Integrate Merge Nightly Release Renew

Code Coverage

Latest Stable Version Total Downloads Monthly Downloads

This project provides a composer package and a Phar archive with an extension for reporting phpunit/phpunit test execution details to agents.

Example

After installing and bootstrapping the extension, when running your tests with phpunit/phpunit, the extension will detect whether an agent is running the tests and replace the default output with test execution details that are easier for agents to consume.

When tests pass, the extension outputs:

{
    "result": "success",
    "summary": {
        "assertions": 5,
        "errors": 0,
        "failures": 0,
        "tests": 5,
        "warnings": 0
    }
}

When tests fail (exit code 1), the extension outputs:

{
    "result": "failure",
    "summary": {
        "assertions": 5,
        "errors": 0,
        "failures": 2,
        "tests": 5,
        "warnings": 0
    },
    "details": {
        "failures": [
            {
                "file": "/path/to/ExampleTest.php",
                "line": 27,
                "message": "Failed asserting that false is true.",
                "test": "Namespace\\ExampleTest::testFailing"
            },
            {
                "actual": "bar",
                "diff": "--- Expected\n+++ Actual\n@@ @@\n-'foo'\n+'bar'\n",
                "expected": "foo",
                "file": "/path/to/ExampleTest.php",
                "line": 34,
                "message": "Failed asserting that two strings are identical.",
                "test": "Namespace\\ExampleTest::testComparisonFailing"
            }
        ]
    }
}

When tests fail with comparison assertions, the actual, diff, and expected fields provide details about the mismatch. These fields are only present for comparison failures.

When tests error (exit code 2), the extension outputs:

{
    "result": "exception",
    "summary": {
        "assertions": 5,
        "errors": 1,
        "failures": 2,
        "tests": 5,
        "warnings": 0
    },
    "details": {
        "errors": [
            {
                "file": "/path/to/ExampleTest.php",
                "line": 32,
                "message": "Something went wrong.",
                "test": "Namespace\\ExampleTest::testErroring"
            }
        ],
        "failures": [
            {
                "file": "/path/to/ExampleTest.php",
                "line": 27,
                "message": "Failed asserting that false is true.",
                "test": "Namespace\\ExampleTest::testFailing"
            },
            {
                "actual": "bar",
                "diff": "--- Expected\n+++ Actual\n@@ @@\n-'foo'\n+'bar'\n",
                "expected": "foo",
                "file": "/path/to/ExampleTest.php",
                "line": 34,
                "message": "Failed asserting that two strings are identical.",
                "test": "Namespace\\ExampleTest::testComparisonFailing"
            }
        ]
    }
}

The JSON output conforms to the JSON schema included in this package.

Agent Detection

The extension uses ergebnis/agent-detector to detect the presence of agents.

Compatibility

The extension is compatible with the following versions of phpunit/phpunit:

Installation

Installation with composer

Run

composer require --dev ergebnis/phpunit-agent-reporter

to install ergebnis/phpunit-agent-reporter as a composer package.

Installation as Phar

Download phpunit-agent-reporter.phar from the latest release.

Usage

Bootstrapping the extension

Before the extension can report test execution details in phpunit/phpunit, you need to bootstrap it.

Bootstrapping the extension as a composer package

To bootstrap the extension as a composer package when using

  • phpunit/phpunit:^13.0.0
  • phpunit/phpunit:^12.0.0
  • phpunit/phpunit:^11.0.0
  • phpunit/phpunit:^10.0.0

adjust your phpunit.xml configuration file and configure the

 <phpunit
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
     bootstrap="vendor/autoload.php"
 >
+    <extensions>
+        <bootstrap class="Ergebnis\PHPUnit\AgentReporter\Extension"/>
+    </extensions>
     <testsuites>
         <testsuite name="unit">
             <directory>test/Unit/</directory>
         </testsuite>
     </testsuites>
 </phpunit>

Bootstrapping the extension as a PHAR

To bootstrap the extension as a PHAR when using

  • phpunit/phpunit:^13.0.0
  • phpunit/phpunit:^12.0.0
  • phpunit/phpunit:^11.0.0
  • phpunit/phpunit:^10.0.0

adjust your phpunit.xml configuration file and configure the

 <phpunit
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
     bootstrap="vendor/autoload.php"
+    extensionsDirectory="directory/where/you/saved/the/extension/phars"
 >
+    <extensions>
+        <bootstrap class="Ergebnis\PHPUnit\AgentReporter\Extension"/>
+    </extensions>
     <testsuites>
         <testsuite name="unit">
             <directory>test/Unit/</directory>
         </testsuite>
     </testsuites>
 </phpunit>

Changelog

The maintainers of this project record notable changes to this project in a changelog.

Contributing

The maintainers of this project suggest following the contribution guide.

Code of Conduct

The maintainers of this project ask contributors to follow the code of conduct.

General Support Policy

The maintainers of this project provide limited support.

You can support the maintenance of this project by sponsoring @ergebnis.

PHP Version Support Policy

This project currently supports the following PHP versions:

The maintainers of this project add support for a PHP version following its initial release and may drop support for a PHP version when it has reached its end of life.

Security Policy

This project has a security policy.

License

This project uses the MIT license.

Credits

This package is inspired by nunomaduro/pao, originally licensed under MIT by Nuno Maduro.

Social

Follow @localheinz and @ergebnis on Twitter.

About

🤖 Provides an extension for reporting phpunit/phpunit test execution details to agents.

Topics

Resources

License

Code of conduct

Contributing

Security policy

Stars

Watchers

Forks

Packages

 
 
 

Contributors