Chapter15.Logging

PHPUnit supports the logging of test results in several formats.

XML Format

The XML format supported by PHPUnit is loosely based upon the one used by the JUnit task for Apache Ant. The following example shows the XML logfile generated for the tests in ArrayTest:

<?xml version="1.0" encoding="UTF-8"?>
<testsuites>
  <testsuite name="ArrayTest" tests="2" failures="0" errors="0" time="0.020026">
    <testcase name="testNewArrayIsEmpty" class="ArrayTest" time="0.014449"/>
    <testcase name="testArrayContainsAnElement" class="ArrayTest" time="0.005577"/>
  </testsuite>
</testsuites>

The following XML logfile was generated for two tests, testFailure and testError, of a test-case class named FailureErrorTest and shows how failures and errors are denoted.

<?xml version="1.0" encoding="UTF-8"?>
<testsuites>
  <testsuite name="FailureErrorTest" tests="2" failures="1" errors="1" time="0.013603">
    <testcase name="testFailure" class="FailureErrorTest" time="0.011872">
      <failure message="" type="PHPUnit_Framework_AssertionFailedError"></failure>
    </testcase>
    <testcase name="testError" class="FailureErrorTest" time="0.001731">
      <error message="" type="Exception"></error>
    </testcase>
  </testsuite>
</testsuites>

JavaScript Object Notation (JSON)

The JavaScript Object Notation (JSON) is a lightweight data-interchange format. The following example shows the JSON messages generated for the tests in ArrayTest:

{"event":"suiteStart","suite":"ArrayTest","tests":2}
{"event":"test","suite":"ArrayTest",
 "test":"testNewArrayIsEmpty(ArrayTest)","status":"pass",
 "time":0.000460147858,"trace":[],"message":""}
{"event":"test","suite":"ArrayTest",
 "test":"testArrayContainsAnElement(ArrayTest)","status":"pass",
 "time":0.000422954559,"trace":[],"message":""}

The following JSON messages were generated for two tests, testFailure and testError, of a test-case class named FailureErrorTest and show how failures and errors are denoted.

{"event":"suiteStart","suite":"FailureErrorTest","tests":2}
{"event":"test","suite":"FailureErrorTest",
 "test":"testFailure(FailureErrorTest)","status":"fail",
 "time":0.000483989716,"trace":[],"message":""}
{"event":"test","suite":"FailureErrorTest",
 "test":"testError(FailureErrorTest)","status":"error",
 "time":0.000466108322,"trace":[],"message":""}

Test Anything Protocol (TAP)

The Test Anything Protocol (TAP) is Perl's simple text-based interface between testing modules. The following example shows the TAP logfile generated for the tests in ArrayTest:

# TestSuite "ArrayTest" started.
ok 1 - testNewArrayIsEmpty(ArrayTest)
ok 2 - testArrayContainsAnElement(ArrayTest)
# TestSuite "ArrayTest" ended.
1..2

The following TAP logfile was generated for two tests, testFailure and testError, of a test-case class named FailureErrorTest and shows how failures and errors are denoted.

# TestSuite "FailureErrorTest" started.
not ok 1 - Failure: testFailure(FailureErrorTest)
not ok 2 - Error: testError(FailureErrorTest)
# TestSuite "FailureErrorTest" ended.
1..2

GraphViz Markup

PHPUnit can generate a description of the test result as a graph that can be rendered to diagrams in several useful formats such as images using the GraphViz tools.

phpunit --log-graphviz ArrayTest.dot ArrayTest
PHPUnit 3.1.0 by Sebastian Bergmann.

..

Time: 00:00

OK (2 tests)

The following example shows the GraphViz markup generated (and saved to the ArrayTest.dot file in the current directory) for the tests in ArrayTest:

digraph G {
  graph [ overlap="scale", splines="true", sep=".1", fontsize="8" ];
  "ArrayTest" [ color="green" ];
  subgraph "cluster_ArrayTest" {
    label="";
    "testNewArrayIsEmpty" [ color="green" ];
    "testArrayContainsAnElement" [ color="green" ];
  }

  "ArrayTest" -> "testNewArrayIsEmpty";
  "ArrayTest" -> "testArrayContainsAnElement";
}

We can now use the dot command-line tool that is part of the GraphViz software suite to generate a graphic from this markup:

dot -T png -o ArrayTest.png ArrayTest.dot

Figure 15.1 shows the graph representation of the test result rendered from the GraphViz markup above.

Figure15.1.Graph representation of the test result

Graph representation of the test result


Successfull tests are displayed with a green border, failures and errors with a red border, and incomplete or skipped tests with a yellow border. A parent node, such as test suite, is displayed with a non-green border if it has a child node, such as a test, that was not successfull.