2012-02-29 11 views
23

यूनिटस्टेस्ट सभी परीक्षणों को चलाने पर केवल कुल समय प्रस्तुत करता है लेकिन प्रत्येक परीक्षण पर अलग-अलग समय नहीं बिताता है।unittest का उपयोग करते समय प्रत्येक परीक्षण पर बिताए गए समय को कैसे जानें?

unittest का उपयोग करते समय प्रत्येक परीक्षण का समय कैसे जोड़ें?

उत्तर

30

मुझे लगता है कि अब यह संभव नहीं है: http://bugs.python.org/issue4080

लेकिन आप कुछ इस तरह कर सकते हैं:

import unittest 
import time 

class SomeTest(unittest.TestCase): 
    def setUp(self): 
     self.startTime = time.time() 

    def tearDown(self): 
     t = time.time() - self.startTime 
     print "%s: %.3f" % (self.id(), t) 

    def testOne(self): 
     time.sleep(1) 
     self.assertEquals(int('42'), 42) 

    def testTwo(self): 
     time.sleep(2) 
     self.assertEquals(str(42), '42') 

if __name__ == '__main__': 
    suite = unittest.TestLoader().loadTestsFromTestCase(SomeTest) 
    unittest.TextTestRunner(verbosity=0).run(suite) 

परिणाम: pinnochio extension साथ

__main__.SomeTest.testOne: 1.001 
__main__.SomeTest.testTwo: 2.002 
---------------------------------------------------------------------- 
Ran 2 tests in 3.003s 

OK 
+0

मुझे लगता है कि यह कुछ बेस क्लास में परिभाषित करना अच्छा होगा ताकि कोई आसानी से इसे मिश्रण कर सके। –

+0

Definitelly। यह एक संभावना का केवल उदाहरण है। – horejsek

+0

@szeitlin नहीं, यह विधि है: https://docs.python.org/3/library/unittest.html#unittest.TestCase.id – horejsek

5

Nose परीक्षण, एक stopwatch विकल्प जो आप इस दे देंगे है अगर नाक आप के लिए एक विकल्प है ।

इसमें अनूठे निसर का उपयोग करने के लिए अन्य उपयोगी सुविधाएं और प्लगइन भी शामिल हैं।

5

यहां हॉरेजेसेक के उत्तर से लिपि की विविधता है। यह बंदर-पैच django टेस्टकेस होगा ताकि प्रत्येक टेस्टकेस अपना कुल चलने वाला समय दे।

आप इस क्रिप्ट को रूट पैकेज के __init__.py में रख सकते हैं, जहां आपकी सेटिंग्स.py रहता है। उसके बाद आप ./mange.py परीक्षण के साथ परीक्षण चला सकते हैं -s केवल कमांड लाइन के साथ

from django import test 
import time 


@classmethod 
def setUpClass(cls): 
    cls.startTime = time.time() 


@classmethod 
def tearDownClass(cls): 
    print "\n%s.%s: %.3f" % (cls.__module__, cls.__name__, time.time() - cls.startTime) 


test.TestCase.setUpClass = setUpClass 
test.TestCase.tearDownClass = tearDownClass 
2

समाधान:

1/स्थापित nose (लोकप्रिय विकल्प परीक्षण धावक) और विस्तार pinnochio

$ pip install nose pinnochio 

2/समय रिकॉर्डिंग (बार फ़ाइल .nose-stopwatch-times में सहेजे जाते हैं)

+०१२३५१६४१० के साथ चलाने के परीक्षण
$ nosetests --with-stopwatch 

3/प्रदर्शन परीक्षण नामों को कम समय के अनुसार क्रमबद्ध:

$ 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 
+0

नाक अब कुछ सालों से परेशान है। हर कोई py.test का उपयोग करता है और ठीक है। –

+0

मुझे लगता है कि यह प्रिंट 'ms' जब इसे प्रिंट करना चाहिए' –

0

आप django-slowtests उपयोग कर सकते हैं, जो इस तरह उत्पादन प्रदान करता है:

$ python manage.py test 
Creating test database for alias 'default'... 
.......... 
---------------------------------------------------------------------- 
Ran 10 tests in 0.413s 

OK 
Destroying test database for alias 'default'... 

Ten slowest tests: 
0.3597s test_detail_view_with_a_future_poll (polls.tests.PollIndexDetailTests) 
0.0284s test_detail_view_with_a_past_poll (polls.tests.PollIndexDetailTests) 
0.0068s test_index_view_with_a_future_poll (polls.tests.PollViewTests) 
0.0047s test_index_view_with_a_past_poll (polls.tests.PollViewTests) 
0.0045s test_index_view_with_two_past_polls (polls.tests.PollViewTests) 
0.0041s test_index_view_with_future_poll_and_past_poll (polls.tests.PollViewTests) 
0.0036s test_index_view_with_no_polls (polls.tests.PollViewTests) 
0.0003s test_was_published_recently_with_future_poll (polls.tests.PollMethodTests) 
0.0002s test_was_published_recently_with_recent_poll (polls.tests.PollMethodTests) 
0.0002s test_was_published_recently_with_old_poll (polls.tests.PollMethodTests) 

आप django_slowtests/test_runner.py को देखें, तो आप भी खुद को अनुकूलित कर खुद तकनीक

संबंधित मुद्दे

 संबंधित मुद्दे