PHPUnit print tests execution time

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

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

9条回答
  •  面向向阳花
    2021-02-11 21:41

    Here's a complete example based on the idea from edorians answer. Tested on PHPunit 4.

    Create the following PHP class:

    class ProfilingTestListener extends PHPUnit_Framework_BaseTestListener
    {
        public function endTest(PHPUnit_Framework_Test $test, $time)
        {
            printf("Test '%s' ended.\tTotal time %s s.\tTest time %s s.\n",
                str_pad($test->toString(), 50),
                number_format($test->getTestResultObject()->time(), 3),
                number_format($time, 3)
            );
        }
    }
    

    Add the following to your phpunit.xml:

    
        ...
        
            
        
        ...
    
    

    Example output:

    PHPUnit 4.7.7 by Sebastian Bergmann and contributors.
    
    Test 'FooTest::testFoo              ' ended.    Total time 2.050 s. Test time 0.026 s. 
    .Test 'FooTest::testBar             ' ended.    Total time 2.077 s. Test time 1.000 s.
    .Test 'FooTest::testBar2            ' ended.    Total time 3.077 s. Test time 0.730 s.
    

提交回复
热议问题