PHPUnit print tests execution time

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

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

9条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-02-11 21:25

    Many of the current answers discuss how to access and analyze the duration times in the log file. I will share two ways to modify the CLI output in phpUnit version 3.7.38 (which is what Travis-CI uses for PHP by default), building on @edorian's incomplete answer.


    Use a custom printer to override the CLI output. I can't find any documentation for printers but they appear to be supported. You can see which methods are available in the source code.

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

    Then add these lines as attributes to phpunit in the phpunit.xml file:

    printerFile="path/to/TestDurationPrinter.php"
    printerClass="TestDurationPrinter"
    

    You can also use the --printer CLI option but that doesn't play well with namespaces.


    You can add to the CLI output, as opposed to overriding it, with a TestListener by implementing the PHPUnit_Framework_TestListener interface (this is the same interface that printers use). This will still print ., S, and F so be sure to account for that, if you desire.

    class TestDurationListener implements PHPUnit_Framework_TestListener
    {
        public function endTest(PHPUnit_Framework_Test $test, $time)
        {
            printf("Test '%s' ended and took %s seconds.\n", 
               $test->getName(),
               $time
            );
        }
    
        public function addError(PHPUnit_Framework_Test $test, Exception $e, $time)
        {
        }
    
        public function addFailure(PHPUnit_Framework_Test $test, PHPUnit_Framework_AssertionFailedError $e, $time)
        {
        }
    
        public function addIncompleteTest(PHPUnit_Framework_Test $test, Exception $e, $time)
        {
        }
    
        public function addRiskyTest(PHPUnit_Framework_Test $test, Exception $e, $time)
        {
        }
    
        public function addSkippedTest(PHPUnit_Framework_Test $test, Exception $e, $time)
        {
        }
    
        public function startTest(PHPUnit_Framework_Test $test)
        {
        }
    
        public function startTestSuite(PHPUnit_Framework_TestSuite $suite)
        {
        }
    
        public function endTestSuite(PHPUnit_Framework_TestSuite $suite)
        {
        }
    }
    

    In version 3.8 and above there is a PHPUnit_Framework_BaseTestListener that can be extended so that you only define the methods you want to override.

    class TestDurationListener extends PHPUnit_Framework_BaseTestListener
    {
        public function endTest(PHPUnit_Framework_Test $test, $time)
        {
            printf("Test '%s' ended.\n", $test->getName());
        }
    }
    

    To include your new listener, add these lines to your phpunit.xml file:

    
        
    
    

提交回复
热议问题