michael@0: # This Source Code Form is subject to the terms of the Mozilla Public michael@0: # License, v. 2.0. If a copy of the MPL was not distributed with this file, michael@0: # You can obtain one at http://mozilla.org/MPL/2.0/. michael@0: michael@0: import math michael@0: import time michael@0: import unittest michael@0: michael@0: from moztest.results import TestContext, TestResult, TestResultCollection michael@0: michael@0: michael@0: class Result(unittest.TestCase): michael@0: michael@0: def test_results(self): michael@0: self.assertRaises(AssertionError, michael@0: lambda: TestResult('test', result_expected='hello')) michael@0: t = TestResult('test') michael@0: self.assertRaises(ValueError, lambda: t.finish(result='good bye')) michael@0: michael@0: def test_time(self): michael@0: now = time.time() michael@0: t = TestResult('test') michael@0: time.sleep(1) michael@0: t.finish('PASS') michael@0: duration = time.time() - now michael@0: self.assertTrue(math.fabs(duration - t.duration) < 1) michael@0: michael@0: def test_custom_time(self): michael@0: t = TestResult('test', time_start=0) michael@0: t.finish(result='PASS', time_end=1000) michael@0: self.assertEqual(t.duration, 1000) michael@0: michael@0: michael@0: class Collection(unittest.TestCase): michael@0: michael@0: def setUp(self): michael@0: c1 = TestContext('host1') michael@0: c2 = TestContext('host2') michael@0: c3 = TestContext('host2') michael@0: c3.os = 'B2G' michael@0: c4 = TestContext('host1') michael@0: michael@0: t1 = TestResult('t1', context=c1) michael@0: t2 = TestResult('t2', context=c2) michael@0: t3 = TestResult('t3', context=c3) michael@0: t4 = TestResult('t4', context=c4) michael@0: michael@0: self.collection = TestResultCollection('tests') michael@0: self.collection.extend([t1, t2, t3, t4]) michael@0: michael@0: def test_unique_contexts(self): michael@0: self.assertEqual(len(self.collection.contexts), 3) michael@0: michael@0: if __name__ == '__main__': michael@0: unittest.main()