[Piglit] [PATCH 1/3] framework/results.py: Add a get_result method to TestrunResult
baker.dylan.c at gmail.com
baker.dylan.c at gmail.com
Mon Oct 19 15:34:43 PDT 2015
From: Dylan Baker <baker.dylan.c at gmail.com>
This provides a single source of getting results from tests or subtests,
and hides the magic of differentiating the two. This will make it easier
to work with subtests in the Summary module, since we can easily know
that we've checked subtests.
Signed-off-by: Dylan Baker <dylanx.c.baker at intel.com>
---
framework/results.py | 20 ++++++++++++++++++++
framework/tests/results_tests.py | 28 ++++++++++++++++++++++++++++
2 files changed, 48 insertions(+)
diff --git a/framework/results.py b/framework/results.py
index 5354a32..ac371d4 100644
--- a/framework/results.py
+++ b/framework/results.py
@@ -287,6 +287,26 @@ class TestrunResult(object):
self.tests = {}
self.totals = collections.defaultdict(Totals)
+ def get_result(self, key):
+ """Get the result of a test or subtest.
+
+ If neither a test nor a subtest instance exist, then raise the original
+ KeyError generated from looking up <key> in the tests attribute. It is
+ the job of the caller to handle this error.
+
+ Arguments:
+ key -- the key name of the test to return
+
+ """
+ try:
+ return self.tests[key].result
+ except KeyError as e:
+ name, test = grouptools.splitname(key)
+ try:
+ return self.tests[name].subtests[test]
+ except KeyError:
+ raise e
+
def calculate_group_totals(self):
"""Calculate the number of pases, fails, etc at each level."""
for name, result in self.tests.iteritems():
diff --git a/framework/tests/results_tests.py b/framework/tests/results_tests.py
index 8128b38..83848a7 100644
--- a/framework/tests/results_tests.py
+++ b/framework/tests/results_tests.py
@@ -631,3 +631,31 @@ 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')
+
+
+class TestTestrunResult_get_result(object):
+ """Tests for TestrunResult.get_result."""
+ @classmethod
+ def setup_class(cls):
+ tr = results.TestResult('crash')
+ tr.subtests['foo'] = status.PASS
+
+ run = results.TestrunResult()
+ run.tests['sub'] = tr
+ run.tests['test'] = results.TestResult('pass')
+ run.calculate_group_totals()
+
+ cls.inst = run
+
+ def test_get_test(self):
+ """results.TestrunResult.get_result: gets non-subtests"""
+ nt.eq_(self.inst.get_result('test'), 'pass')
+
+ def test_get_subtest(self):
+ """results.TestrunResult.get_result: gets subtests"""
+ nt.eq_(self.inst.get_result(grouptools.join('sub', 'foo')), 'pass')
+
+ @nt.raises(KeyError)
+ def test_get_nonexist(self):
+ """results.TestrunResult.get_result: raises KeyError if test doesn't exist"""
+ self.inst.get_result('fooobar')
--
2.6.1
More information about the Piglit
mailing list