-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtests.py
108 lines (83 loc) · 3.21 KB
/
tests.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
97
98
99
100
101
102
103
104
105
106
107
108
import sys
try:
from StringIO import StringIO
except ImportError:
from io import StringIO
from unittest import TestCase
from pyprofmem.decorators import (
memory_usage, profile_with_memory_usage, PROFILE_SORT_CUMULATIVE, PROFILE_SORT_CALLS,
PROFILE_SORT_STDNAME, PROFILE_SORT_TIME
)
class StdOut(object):
"""Context manager to capture the output of the function calls"""
def __init__(self):
self.__lines = []
def __enter__(self):
self.__stdout = sys.stdout
sys.stdout = self.__strio = StringIO()
return self
def __exit__(self, *args):
self.__lines.extend(self.__strio.getvalue().splitlines())
del self.__strio
sys.stdout = self.__stdout
def __len__(self):
return len(self.__lines)
def __str__(self):
return '\n'.join(self.__lines)
def __getitem__(self, item):
return self.__lines[item]
class Test(TestCase):
@memory_usage
def func_test_memory(self):
for _ in range(10):
pass
@profile_with_memory_usage()
def func_test_profile_without_arguments(self):
text = 'func_test_profile_without_arguments'
return text
@profile_with_memory_usage(sort_type=PROFILE_SORT_CUMULATIVE)
def func_test_profile_cumulative(self):
text = 'func_test_profile_cumulative'
return text
@profile_with_memory_usage(sort_type=PROFILE_SORT_CALLS)
def func_test_profile_calls(self):
text = 'func_test_profile_calls'
return text
@profile_with_memory_usage(sort_type=PROFILE_SORT_STDNAME)
def func_test_profile_stdname(self):
text = 'func_test_profile_stdname'
return text
@profile_with_memory_usage(sort_type=PROFILE_SORT_TIME)
def func_test_profile_time(self):
text = 'func_test_profile_time'
return text
def test_memory(self):
with StdOut() as stdout:
self.func_test_memory()
self.assertEqual(len(stdout), 3)
self.assertEqual(stdout[0].find('func_test_memory') > 0, True)
def test_profile_without_arguments(self):
with StdOut() as stdout:
self.func_test_profile_without_arguments()
self.assertEqual(len(stdout), 12)
self.assertEqual(stdout[-3].find('func_test_profile_without_arguments') > 0, True)
def test_profile_cumulative(self):
with StdOut() as stdout:
self.func_test_profile_cumulative()
self.assertEqual(len(stdout), 12)
self.assertEqual(stdout[-3].find('func_test_profile_cumulative') > 0, True)
def test_profile_calls(self):
with StdOut() as stdout:
self.func_test_profile_calls()
self.assertEqual(len(stdout), 12)
self.assertEqual(stdout[-3].find('func_test_profile_calls') > 0, True)
def test_profile_stdname(self):
with StdOut() as stdout:
self.func_test_profile_stdname()
self.assertEqual(len(stdout), 12)
self.assertEqual(stdout[-3].find('func_test_profile_stdname') > 0, True)
def test_profile_time(self):
with StdOut() as stdout:
self.func_test_profile_time()
self.assertEqual(len(stdout), 12)
self.assertEqual(stdout[-3].find('func_test_profile_time') > 0, True)