Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get detailed test info from PHPUnit testdox

When generating a log file using testdox-html the results shown are simply the names of the test methods that are either strike through text if fail or normal if passed. What I'd like is for the testdox file to generate the error information much like the command line output. Is this at all possible?

like image 709
Kevin Avatar asked Jan 19 '11 10:01

Kevin


1 Answers

The Code that creates the HTML is located in PHPUnit/Util/TestDox/ResultPrinter/HTML.php and sadly I don't know any way to extend it. You can just change it, but then you'd have to repeat that for every update which could get annoying.

Since the output is rather small anyway, I'd go another path:

I'd use the .xml file phpunit output (e.g. phpunit --log-junit foo.xml DemoTest.php ) and use xslt or a dom parser to transform the output into html. It shouldn't be much work and you can customize it very quickly.

I've written a little example using xslt to transform the output.

Test

<?php

class DemoTest extends PHPUnit_Framework_TestCase {

    public function testPass() {
        $this->assertTrue(true);
    }

    public function testFail() {
        $this->assertTrue(false);
    }

}

Create xml output

phpunit --log-junit foo.xml DemoTest.php

The xslt template

<?xml version="1.0"?>

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">
  <html>
  <body>
    <h1>Tests</h1>
      <xsl:for-each select="testsuites/testsuite">
        <h2><xsl:value-of select="@name"/></h2>
        <ul>
        <xsl:for-each select="testcase">
          <li>
            <xsl:value-of select="@name"/>
            <xsl:if test="failure">
                <b>Failed !</b>
                <i><xsl:value-of select="*"/></i>
            </xsl:if>
          </li>
        </xsl:for-each>
        </ul>
      </xsl:for-each>
  </body>
  </html>
</xsl:template>

</xsl:stylesheet>

Transforming the output

xsltproc foo.xsl foo.xml > output.html

What it looks like

<html>
<body>
  <h1>Tests</h1>
    <h2>DemoTest</h2>
      <ul>
        <li>testPass</li>
        <li>testFail<b>Failed !</b>
          <i>DemoTest::testFail
          Failed asserting that &lt;boolean:false&gt; is true.
          /home/edo/DemoTest.php:10
          </i>
       </li>
      </ul>
</body>
</html>

and it should be easily adapted because you can use all the values in the xml file like the runtime of each tests and so on.

Alternatives

You could use a DomParser, just modify the PHPUnit class or maybe someone else has a quicker idea :)

like image 122
edorian Avatar answered Oct 07 '22 18:10

edorian