這足以讓我的(相對時間),但它不適用於並行測試,並且確實有提高時間精度的範圍。儘管如此:
覆蓋通過settings.py
默認亞軍:
TEST_RUNNER = 'myapp.test_runner.MyTestRunner'
從那裏創建自己的test_runner myapp/test_runner.py
:
from django.test.runner import DiscoverRunner
class MyTestRunner(DiscoverRunner):
test_runner = TimedTextTestRunner
,反過來,覆蓋與resultclass:
from unittest.runner import TextTestRunner, TextTestResult
class TimedTextTestRunner(TextTestRunner):
resultclass = TimedTextTestResult
現在結果對象不僅僅是一個結果但很多,所以我們需要一系列時鐘,通過測試來鎖定。然後捕捉時間的考驗開始,並打印出的時間過去了,當它打印成功的字符串:
class TimedTextTestResult(TextTestResult):
def __init__(self, *args, **kwargs):
super(TimedTextTestResult, self).__init__(*args, **kwargs)
self.clocks = dict()
def startTest(self, test):
self.clocks[test] = time()
super(TextTestResult, self).startTest(test)
if self.showAll:
self.stream.write(self.getDescription(test))
self.stream.write(" ... ")
self.stream.flush()
def addSuccess(self, test):
super(TextTestResult, self).addSuccess(test)
if self.showAll:
self.stream.writeln("ok-dokey (%.6fs)" % (time() - self.clocks[test]))
elif self.dots:
self.stream.write('.')
self.stream.flush()
這給了我的測試報告看起來像這樣:
test_price_impact (api.tests.TestGroupViews) ... ok-dokey (3.123600s)
test_realised_spread (api.tests.TestGroupViews) ... ok-dokey (6.894571s)
test_sqrt_trade_value (api.tests.TestGroupViews) ... ok-dokey (0.147969s)
test_trade_count_share (api.tests.TestGroupViews) ... ok-dokey (3.124844s)
test_trade_size (api.tests.TestGroupViews) ... ok-dokey (3.134234s)
test_value_share (api.tests.TestGroupViews) ... ok-dokey (2.939364s)
感謝隊友,但它的工作和他們不要用鼻子,數據庫與postgres綁定,遷移對測試非常重要,這些數字是爲了提供一個例子。但是,嘿,肯定比沒有任何人的迴應好。我很欣賞這種努力。 –