简体   繁体   中英

How to profile code during unit test with Nosetests?

I have looked at --with-timer and --with-profile to profile my tests but these options either give too little info or too much info.

Example, if I have the following test:

def test_foo():
    do_something()
    do_something_else()

I just want to profile how long each function call or each line within the test took. So the output will be something like:

   do_something() : .5 seconds
   do_something_else() : .5 seconds
   test_foo() : 1 seconds

Inspired by this talk , you could user the Xunit plugin and post-process its xml

Use --with-xunit and the following code to process nosetests.xml

from xml.etree.cElementTree import parse 
from operator import itemgetter 

elems = parse(open("nosetests.xml")).getiterator("testcase") 
tests = sorted(((e.get("name"), int(e.get("time"))) for e in elems), 
               key=itemgetter(1), reverse=1) 

for test in (test for test in tests if test[1]): 
    print "%s: %s sec" % test 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM