简体   繁体   English

如何在Nosetests的单元测试期间分析代码?

[英]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. 我已经看过--with-timer--with-profile来描述我的测试,但是这些选项给出的信息太少或太多。

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 受此演讲的启发,您可以使用Xunit插件并对其XML进行后处理

Use --with-xunit and the following code to process nosetests.xml 使用--with-xunit和以下代码来处理鼻子测试.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 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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