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

前端 未结 7 1517
-上瘾入骨i
-上瘾入骨i 2021-02-05 00:37

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?

7条回答
  •  青春惊慌失措
    2021-02-05 01:14

    Solution with command-line only:

    1/ install nose (popular alternative test-runner) and extension pinnochio

    $ pip install nose pinnochio
    

    2/ run tests with time recording (times are saved in file .nose-stopwatch-times)

    $ nosetests --with-stopwatch
    

    3/ display tests names sorted by decreasing time:

    $ python -c "import pickle,operator,signal; signal.signal(signal.SIGPIPE, signal.SIG_DFL); print '\n'.join(['%.03fs: %s'%(v[1],v[0]) for v in sorted(pickle.load(open('.nose-stopwatch-times','r')).items(), key=operator.itemgetter(1), reverse=True)])" | less
    

提交回复
热议问题