If you use PHPUnit to manage and run your unit tests, the New Relic PHP agent can automatically capture the test summary results and send them to New Relic as an event where you can query and visualize test data at a glance. This feature was released in version 6.7.0 of the PHP Agent and supports PHPUnit versions 3.7 to 8.x
To enable PHPUnit test events:
- Find or add the
newrelic.phpunit_events.enabledsetting in your
newrelic.inifile and set it to
- Restart your web server (apache, PHP-FPM, Nginx, etc) for the setting change to take effect.
Exception messages are collected and sent with events. Also, if you use PHPUnit's
--disallow-test-output option, which flags tests that print output as "risky," the test event will include the offending output.
When enabled, the PHP Agent detects PHPUnit commands and populates New Relic with two event types that contain data for the test suite (named
TestSuite) and individual tests (named
Test). You can query the data with NRQL queries and build a dashboard of information important to you.
To query your test events, use
FROM TestSuite and
FROM Test when specifying your
SELECT uniqueCount(name) FROM TestSuite
Example of a PHPUnit test dashboard created by querying
A dashboard of test summary data allows you to quickly see a snapshot of overall test success as well as dig into failing suites to determine their cause. These examples illustrate the kinds of widgets you can create with both
Test event types.
Because a PHPUnit test suite is linked to individual tests via its run ID, you can use
FACET widgets to filter results for a specific test run.
For example, if you created a widget with the most recent failing test suites and linked it to the current dashboard, you could click on a test and the surrounding widgets would update with information for only that test suite run.
FACET widget (top-left) linked to the current dashboard allows you to drill into failing test suites.
In the above example, you can see that by clicking on run
5bb37ccee2a1dbc7, we learn that one of two tests,
testFoo, failed. Here are the NRQL queries that made up this example:
Recent unsuccessful suites:
SELECT latest(timestamp), latest(name) FROM TestSuite WHERE successful IS false FACET runId
Last unsuccessful suite:
SELECT host, name, duration * 1000 AS 'duration (ms)', assertionCount, testCount, passedCount, failedCount, incompleteCount, skippedCount, errorCount, riskyCount, warningCount FROM TestSuite WHERE successful IS false LIMIT 1
Last unsuccessful test:
SELECT host, name, testSuiteName, duration * 1000 AS 'duration (ms)', outcome, assertionCount, message FROM Test WHERE outcome != 'passed' LIMIT 1
Test events contain the following attributes you can query against:
- Suggest a change and learn how to contribute to our PHP agent open source repository.
Additional documentation resources include: