test.py (1350B)
1 # This Source Code Form is subject to the terms of the Mozilla Public 2 # License, v. 2.0. If a copy of the MPL was not distributed with this file, 3 # You can obtain one at http://mozilla.org/MPL/2.0/. 4 5 import math 6 import time 7 8 import mozunit 9 import pytest 10 from moztest.results import TestContext, TestResult, TestResultCollection 11 12 13 def test_results(): 14 with pytest.raises(AssertionError): 15 TestResult("test", result_expected="hello") 16 t = TestResult("test") 17 with pytest.raises(ValueError): 18 t.finish(result="good bye") 19 20 21 def test_time(): 22 now = time.time() 23 t = TestResult("test") 24 time.sleep(1) 25 t.finish("PASS") 26 duration = time.time() - now 27 assert math.fabs(duration - t.duration) < 1 28 29 30 def test_custom_time(): 31 t = TestResult("test", time_start=0) 32 t.finish(result="PASS", time_end=1000) 33 assert t.duration == 1000 34 35 36 def test_unique_contexts(): 37 c1 = TestContext("host1") 38 c2 = TestContext("host2") 39 c3 = TestContext("host2") 40 c4 = TestContext("host1") 41 42 t1 = TestResult("t1", context=c1) 43 t2 = TestResult("t2", context=c2) 44 t3 = TestResult("t3", context=c3) 45 t4 = TestResult("t4", context=c4) 46 47 collection = TestResultCollection("tests") 48 collection.extend([t1, t2, t3, t4]) 49 50 assert len(collection.contexts) == 2 51 52 53 if __name__ == "__main__": 54 mozunit.main()