python https://hackernoon.com/timing-tests-in-python-for-fun-and-profit-1663144571

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python https://hackernoon.com/timing-tests-in-python-for-fun-and-profit-1663144571相关的知识,希望对你有一定的参考价值。

import time
from unittest.runner import TextTestResult


class TimeLoggingTestResult(TextTestResult):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.test_timings = []
    def startTest(self, test):
        self._test_started_at = time.time()
        super().startTest(test)
    def addSuccess(self, test):
        elapsed = time.time() - self._test_started_at
        name = self.getDescription(test)
        self.test_timings.append((name, elapsed))
        super().addSuccess(test)
    def getTestTimings(self):
        return self.test_timings

class TimeLoggingTestRunner(unittest.TextTestRunner):
    
    def __init__(self, slow_test_threshold=0.3, *args, **kwargs):
        self.slow_test_threshold = slow_test_threshold
        return super().__init__(
            resultclass=TimeLoggingTestResult,
            *args,
            **kwargs,
        )
    def run(self, test):
        result = super().run(test)
        self.stream.writeln(
            "\nSlow Tests (>{:.03}s):".format(
                self.slow_test_threshold))
        for name, elapsed in result.getTestTimings():
            if elapsed > self.slow_test_threshold:
                self.stream.writeln(
                    "({:.03}s) {}".format(
                        elapsed, name))
        return result

# for django

from django.test.runner import DiscoverRunner

class TimeLoggingTestRunnerForDjango(DiscoverRunner):
    def get_resultclass(self):
        return TimeLoggingTestResult

以上是关于python https://hackernoon.com/timing-tests-in-python-for-fun-and-profit-1663144571的主要内容,如果未能解决你的问题,请参考以下文章

2021 年最值得学习的 5 大机器学习编程语言!

使用tensorflow的retrain.py训练图片分类器

技术文章阅读-Escaping a Python sandbox with a memory corruption bug

如何使用 StackNavigator 创建动态路由

如何简单的发布一个react组件npm包

比特币和Blockchain的价值 - 给大家扫盲