Created
October 27, 2017 13:55
-
-
Save chidindu-ogbonna/76e7d538dd0eee7e5428949e6ff8867e to your computer and use it in GitHub Desktop.
Using Coverage in Flask framework, to be run along with the tests
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Created with a NoneType | |
cov = None | |
if os.environ.get('COVERAGE'): | |
import coverage | |
# an instance of the coverage.coverage() class is created, using a probable "from_" function of the Coverage class | |
cov = coverage.coverage(branch=True, include='gasify/*') | |
cov.start() | |
app = create_app('default') | |
manager = Manager(app) | |
migrate = Migrate(app, db) | |
@manager.command | |
# using flask scripts, we can add a Boolean argument to the custom commands created, in this case coverage=False | |
def test(coverage=False): | |
"""Run the unittest""" | |
# when the "COVERAGE" env variable is set, the scripts is restarted | |
# and the cov.start() method is called again | |
if coverage and not os.environ.get('COVERAGE'): | |
import sys | |
os.environ['COVERAGE'] = '1' | |
os.execvp(sys.executable, [sys.executable] + sys.argv) | |
import unittest | |
tests = unittest.TestLoader().discover('tests') | |
unittest.TextTestRunner(verbosity=2).run(tests) | |
if cov: | |
# after the "cov" variable is no longer None, and the Coverage instance has been created, the cov.stop() is called | |
cov.stop() | |
# then the coverage results is saved | |
cov.save() | |
print('Coverage summary: ') | |
# Generate the results, on the CLI | |
cov.report() | |
# the basedir is created to point to the location of this file | |
basedir = os.path.abspath(os.path.dirname(__file__)) | |
# covdir is the location of the coverage directory within the basedir | |
covdir = os.path.join(basedir, 'tmp/coverage') | |
# html report is generated at the covdir | |
cov.html_report(directory=covdir) | |
print('HTML version: file://%s/index.html' % covdir) | |
# erase the results | |
cov.erase() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment