pythonperformanceunit-testing

How to know time spent on each test when using unittest?


Unittest presents only total time spent on running all tests but does not present time spent on each test separately.

How to add timing of each test when using unittest?


Solution

  • unittest now supports this natively (to the millisecond) with the --durations 0 option.

    $ python -m unittest Lib.test.test_ftplib --durations 0
    ..........................................s.............................................
    Slowest test durations
    ----------------------------------------------------------------------
    0.000s     test__all__ (Lib.test.test_ftplib.MiscTestCase)
    0.002s     test_pwd (Lib.test.test_ftplib.TestFTPClass)
    ....
    0.140s     test_storbinary_rest (Lib.test.test_ftplib.TestTLS_FTPClassMixin)
    0.148s     test_data_connection (Lib.test.test_ftplib.TestTLS_FTPClass)
    0.154s     test_storlines (Lib.test.test_ftplib.TestTLS_FTPClassMixin)
    0.175s     test_retrbinary_rest (Lib.test.test_ftplib.TestTLS_FTPClassMixin)
    0.204s     test_with_statement (Lib.test.test_ftplib.TestFTPClass)
    0.208s     test_with_statement (Lib.test.test_ftplib.TestTLS_FTPClassMixin)
    0.880s     test_mlsd (Lib.test.test_ftplib.TestFTPClass)
    1.281s     test_mlsd (Lib.test.test_ftplib.TestTLS_FTPClassMixin)
    
    ----------------------------------------------------------------------
    Ran 88 tests in 5.396s
    
    OK (skipped=1)