PHPUnit print tests execution time

前端 未结 9 1406
天涯浪人
天涯浪人 2021-02-11 21:04

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

9条回答
  •  别那么骄傲
    2021-02-11 21:48

    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

    
    
      
        
        
          DemoTest::testFail
    Failed asserting that <boolean:false> is true.
    
    /home/edo/foo.php:9
    
        
      
    
    

    and with an transformation like this:

    
    
    
      
      
        

    Tests

    • : Failed !

    you get lines showing you:

  • testPass : 0.003801
  • (the HTML is just an example, it should be easily adaptable).

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

提交回复
热议问题