From 878fa4eb89236875cae132d8633aa6245e895cda Mon Sep 17 00:00:00 2001 From: Dylan Baker Date: Wed, 7 Oct 2015 23:31:44 -0700 Subject: framework: Add a TimeAttribute class This class will provide a new interface for time, one that stores the start and stop times, rather than the total time. It then provides methods for getting the total and the delta (as used in the summary). This commit adds the new class and tests for the class, but doesn't start plugging it in yet. The goal of this change is to allow post-processing to determine which tests were running in parallel. Reviewed-by: Mark Janes Signed-off-by: Dylan Baker --- framework/backends/json.py | 1 + framework/results.py | 38 ++++++++++++++++++++++++++++++++++++++ framework/tests/results_tests.py | 34 ++++++++++++++++++++++++++++++++-- 3 files changed, 71 insertions(+), 2 deletions(-) diff --git a/framework/backends/json.py b/framework/backends/json.py index 935480b79..4cc7957b0 100644 --- a/framework/backends/json.py +++ b/framework/backends/json.py @@ -51,6 +51,7 @@ _DECODER_TABLE = { 'Subtests': results.Subtests, 'TestResult': results.TestResult, 'TestrunResult': results.TestrunResult, + 'TimeAttribute': results.TimeAttribute, 'Totals': results.Totals, } diff --git a/framework/results.py b/framework/results.py index 26f438066..7eca5bdec 100644 --- a/framework/results.py +++ b/framework/results.py @@ -25,6 +25,7 @@ from __future__ import print_function, absolute_import import collections import copy +import datetime from framework import status, exceptions, grouptools @@ -103,6 +104,43 @@ class StringDescriptor(object): # pylint: disable=too-few-public-methods raise NotImplementedError +class TimeAttribute(object): + """Attribute of TestResult for time. + + This attribute provides a couple of nice helpers. It stores the start and + end time and provides methods for getting the total and delta of the times. + + """ + __slots__ = ['start', 'end'] + + def __init__(self, start=0.0, end=0.0): + self.start = start + self.end = end + + @property + def total(self): + return self.end - self.start + + @property + def delta(self): + return str(datetime.timedelta(seconds=self.total)) + + def to_json(self): + return { + 'start': self.start, + 'end': self.end, + '__type__': 'TimeAttribute', + } + + @classmethod + def from_dict(cls, dict_): + dict_ = copy.copy(dict_) + + if '__type__' in dict_: + del dict_['__type__'] + return cls(**dict_) + + class TestResult(object): """An object represting the result of a single test.""" __slots__ = ['returncode', '_err', '_out', 'time', 'command', 'traceback', diff --git a/framework/tests/results_tests.py b/framework/tests/results_tests.py index 1b11df036..57ca6466e 100644 --- a/framework/tests/results_tests.py +++ b/framework/tests/results_tests.py @@ -1,4 +1,4 @@ -# Copyright (c) 2014 Intel Corporation +# Copyright (c) 2014, 2015 Intel Corporation # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to deal @@ -213,7 +213,7 @@ class TestTestResult_to_json(object): """results.TestResult.to_json: sets the out correctly""" nt.eq_(self.dict['out'], self.json['out']) - def test_out(self): + def test_exception(self): """results.TestResult.to_json: sets the exception correctly""" nt.eq_(self.dict['exception'], self.json['exception']) @@ -581,3 +581,33 @@ class TestTestrunResultFromDict(object): status.Status), msg='Subtests should be type Status, but was "{}"'.format( type(self.test.tests['subtest'].subtests['foo']))) + + +def test_TimeAttribute_to_json(): + """results.TimeAttribute.to_json(): returns expected dictionary""" + baseline = {'start': 0.1, 'end': 1.0} + test = results.TimeAttribute(**baseline) + baseline['__type__'] = 'TimeAttribute' + + nt.assert_dict_equal(baseline, test.to_json()) + + +def test_TimeAttribute_from_dict(): + """results.TimeAttribute.from_dict: returns expected value""" + # Type is included because to_json() adds it. + baseline = {'start': 0.1, 'end': 1.0, '__type__': 'TimeAttribute'} + test = results.TimeAttribute.from_dict(baseline).to_json() + + nt.assert_dict_equal(baseline, test) + + +def test_TimeAttribute_total(): + """results.TimeAttribute.total: returns the difference between end and start""" + test = results.TimeAttribute(1.0, 5.0) + nt.eq_(test.total, 4.0) + + +def test_TimeAttribute_delta(): + """results.TimeAttribute.delta: returns the delta of the values""" + test = results.TimeAttribute(1.0, 5.0) + nt.eq_(test.delta, '0:00:04') -- cgit v1.2.3