-
Notifications
You must be signed in to change notification settings - Fork 1
/
commands.py
46 lines (39 loc) · 1.23 KB
/
commands.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
"""
Flask commands for the SaintsXCTF API.
Author: Andrew Jarombek
Date: 6/22/2019
"""
import os
import coverage
import click
import sys
import unittest
from flask.cli import with_appcontext
cov = None
if os.environ.get('FLASK_COVERAGE'):
cov = coverage.coverage(branch=True, include=['app.py', 'dao/*', 'model/*', 'route/*', 'utils/*'])
cov.start()
@click.command()
@with_appcontext
def test():
"""
Create a Flask command for running unit tests. Execute with 'flask test' from a command line.
"""
if not os.environ.get('FLASK_COVERAGE'):
os.environ['FLASK_COVERAGE'] = '1'
os.execvp(sys.executable, [sys.executable] + sys.argv)
# Create a test runner an execute the test suite
tests = unittest.TestLoader().discover('tests')
runner = unittest.TextTestRunner(verbosity=3)
result: unittest.TestResult = runner.run(tests)
if cov:
cov.stop()
cov.save()
print('Coverage Summary:')
cov.report()
basedir = os.path.abspath(os.path.dirname(__file__))
cov_dir = os.path.join(basedir, 'tmp/coverage')
cov.html_report(directory=cov_dir)
print('HTML version: file://%s/index.html' % cov_dir)
cov.erase()
exit(len(result.errors))