-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcoverage.py
46 lines (37 loc) · 1.36 KB
/
coverage.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
import sys
import tests
import inspect
class TinyCoverage(object):
def __init__(self, file_to_watch):
self.source_file = file_to_watch
self.source_code = open(file_to_watch).readlines()
self.executed_code = []
def trace(self, frame, event, arg):
current_file = inspect.getframeinfo(frame).filename
if self.source_file in current_file and \
(event == "line" or event == "call"):
self.executed_code.append(frame.f_lineno)
return self.trace
def unexecuted_code(self):
skipped = []
for line_num in range(1, len(self.source_code)+1):
if line_num not in self.executed_code:
src = self.source_code[line_num - 1]
if src != "\n":
skipped.append(line_num)
return skipped
def report(self):
skipped = self.unexecuted_code()
percent_skipped = float(len(skipped)) / len(self.source_code)
if skipped:
print "{} line(s) did not execute ({:.0%})".format(len(skipped), percent_skipped)
for line_num in skipped:
print line_num, self.source_code[line_num - 1]
else:
print "100% coverage, go you!"
if __name__ == '__main__':
t = TinyCoverage('example.py')
sys.settrace(t.trace)
tests.run_tests()
sys.settrace(None)
t.report()