-
Notifications
You must be signed in to change notification settings - Fork 196
/
Copy pathtest_timer.py
36 lines (26 loc) · 909 Bytes
/
test_timer.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
import os
import pytest
import time
files = [f for f in os.listdir(os.path.dirname(__file__)) if f.startswith("timer")]
for f in files:
exec("import " + f[:-3] + " as " + f[:-3])
def test_names():
for f in files:
assert "Timer" in dir(eval(f[:-3])), "Timer is not defined in " + f[:-3]
def test_context_manager():
for f in files:
assert hasattr(eval(f[:-3]).Timer, "__enter__"), (
"Timer is not a context manager in " + f[:-3]
)
assert hasattr(eval(f[:-3]).Timer, "__exit__"), (
"Timer is not a context manager in " + f[:-3]
)
def test_time_taken():
for f in files:
with eval(f[:-3]).Timer() as timer:
time.sleep(2)
assert 2.1 >= timer.end_time - timer.start_time >= 2, (
"Timer is not working in " + f[:-3]
)
if __name__ == "__main__":
pytest.main(["-v", __file__])