Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHPUnit print tests execution time

is there a way to print the execution time of each test with PHPUnit?

like image 924
mck89 Avatar asked Mar 04 '11 17:03

mck89


People also ask

How do I run a PHPUnit test?

To run the unit test, click the arrow next to the Run button on the Tool-bar, and select Run As | PHPUnit Test . From the Menu-bar, select Run | Run As | PHPUnit Test . To debug the PHPUnit Test Case, click the arrow next to the debug button on the toolbar, and select Debug As | PHPUnit Test .

What is a PHPUnit test?

PHPUnit is a unit testing framework for the PHP programming language. It is an instance of the xUnit design for unit testing systems that began with SUnit and became popular with JUnit. Even a small software development project usually takes hours of hard work.

Is PHPUnit a framework?

PHPUnit is a programmer-oriented testing framework for PHP. It is an instance of the xUnit architecture for unit testing frameworks. The currently supported versions are PHPUnit 9 and PHPUnit 8.


1 Answers

To add some more ways:


You can write a custom Test listener and add it to the XML file. In that listener you can access the $testResult->time(). Some lines in your phpunit.xml and a 10 line PHP class. Not too much hassle.

class SimpleTestListener implements PHPUnit_Framework_TestListener {     public function endTest(PHPUnit_Framework_Test $test, $time)     {         printf("Test '%s' ended and took %s seconds.\n",             $test->getName(),            $test->time()         );     } } 

If you generate a junit.xml anyways (for CI or while creating code coverage) all the numbers are there anyways and with a simple XSLT you can make those even more readable.

Example junit.xml

<?xml version="1.0" encoding="UTF-8"?> <testsuites>   <testsuite name="DemoTest" file="/home/edo/foo.php" tests="2" assertions="2" failures="1" errors="0" time="0.007727">     <testcase name="testPass" class="DemoTest" file="/home/edo/foo.php" line="4" assertions="1" time="0.003801"/>     <testcase name="testFail" class="DemoTest" file="/home/edo/foo.php" line="8" assertions="1" time="0.003926">       <failure type="PHPUnit_Framework_ExpectationFailedException">DemoTest::testFail Failed asserting that &lt;boolean:false&gt; is true.  /home/edo/foo.php:9 </failure>     </testcase>   </testsuite> </testsuites> 

and with an transformation like this:

<?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:value-of select="@time"/>             <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> 

you get lines showing you: <li>testPass : 0.003801</li> (the HTML is just an example, it should be easily adaptable).

Referencing my own blog post here: https://edorian.github.io/2011-01-19-creating-your-custom-phpunit-output.formats/ for the xslt stuff.

like image 128
edorian Avatar answered Oct 06 '22 06:10

edorian