is there a way to print the execution time of each test with PHPUnit?
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 .
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.
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.
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 <boolean:false> 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With