Skip to content

Commit 01c4f97

Browse files
Chapter 15: Coverage metrics (15a)
1 parent 3a46cc1 commit 01c4f97

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

manage.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
#!/usr/bin/env python
22
import os
3+
COV = None
4+
if os.environ.get('FLASK_COVERAGE'):
5+
import coverage
6+
COV = coverage.coverage(branch=True, include='app/*')
7+
COV.start()
8+
39
from app import create_app, db
410
from app.models import User, Follow, Role, Permission, Post, Comment
511
from flask.ext.script import Manager, Shell
@@ -18,11 +24,25 @@ def make_shell_context():
1824

1925

2026
@manager.command
21-
def test():
27+
def test(coverage=False):
2228
"""Run the unit tests."""
29+
if coverage and not os.environ.get('FLASK_COVERAGE'):
30+
import sys
31+
os.environ['FLASK_COVERAGE'] = '1'
32+
os.execvp(sys.executable, [sys.executable] + sys.argv)
2333
import unittest
2434
tests = unittest.TestLoader().discover('tests')
2535
unittest.TextTestRunner(verbosity=2).run(tests)
36+
if COV:
37+
COV.stop()
38+
COV.save()
39+
print('Coverage Summary:')
40+
COV.report()
41+
basedir = os.path.abspath(os.path.dirname(__file__))
42+
covdir = os.path.join(basedir, 'tmp/coverage')
43+
COV.html_report(directory=covdir)
44+
print('HTML version: file://%s/index.html' % covdir)
45+
COV.erase()
2646

2747

2848
if __name__ == '__main__':

requirements/dev.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@
22
ForgeryPy==0.1
33
Pygments==1.6
44
colorama==0.2.7
5+
coverage==3.7.1
56
httpie==0.7.2
67
requests==2.1.0

0 commit comments

Comments
 (0)