-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_swpy.py
98 lines (72 loc) · 2.19 KB
/
test_swpy.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#!/usr/bin/env python
# coding: utf-8
#
# Filename: test_swpy.py
# Author: Peinan ZHANG
# Created at: 2019-05-09
import time
import unittest
from unittest.mock import Mock
from swpy import Timer
class TimerTestCase(unittest.TestCase):
def test_time_elapsed(self):
with Timer() as t:
time.sleep(1)
self.assertAlmostEqual(t.elapsed, 1, 1)
def test_callback(self):
true_msg = '[test] finish time: 0.50 sec.'
f = Mock()
with Timer(name='test', callback=f):
time.sleep(0.5)
f.assert_called_once_with(true_msg)
def test_start(self):
true_msg = '[test] [exp1] finish time: 0.50 sec.'
f = Mock()
t = Timer(name='test', callback=f)
time.sleep(0.1)
t.start()
time.sleep(0.5)
t.stop('exp1')
self.assertAlmostEqual(t.elapsed, 0.5, 1)
f.assert_called_once_with(true_msg)
def test_stop(self):
true_msg = '[test] [exp1] finish time: 0.50 sec.'
f = Mock()
t = Timer(name='test')
time.sleep(0.5)
t.stop(title='exp1', callback=f)
self.assertAlmostEqual(t.elapsed, 0.5, 1)
f.assert_called_once_with(true_msg)
def test_split(self):
true_msg = '[test] [exp1] split time: 0.50 sec.'
f = Mock()
t = Timer(name='test')
time.sleep(0.2)
t.split()
time.sleep(0.3)
t.split('exp1', callback=f)
time.sleep(0.1)
t.stop()
self.assertAlmostEqual(t.elapsed, 0.6, 1)
f.assert_called_once_with(true_msg)
def test_lap(self):
true_msg = '[test] [exp1] lap time: 0.20 sec.'
f = Mock()
t = Timer(name='test')
time.sleep(0.3)
t.lap()
time.sleep(0.2)
t.lap('exp1', callback=f)
time.sleep(0.1)
t.stop()
self.assertAlmostEqual(t.elapsed, 0.6, 1)
f.assert_called_once_with(true_msg)
def test_wo_title(self):
true_msg = '[test] finish time: 0.50 sec.'
f = Mock()
t = Timer(name='test', callback=f)
time.sleep(0.1)
t.start()
time.sleep(0.5)
t.stop()
f.assert_called_once_with(true_msg)