Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting up coverage.py with flask

I am struggling to get coverage.py to work with my Flask application.

I am trying to set it up via the subprocess instructions: http://nedbatchelder.com/code/coverage/subprocess.html

In my create_app() function (which is an application factory) I have the following:

if settings.FLASK_ENV == 'TEST':
    coverage.process_startup()

In my test suite I have the following:

# Need to add the 'COVERAGE_PROCESS_START' environment variable for subprocesses
if os.getenv('COVERAGE'):
    test_env['COVERAGE_PROCESS_START'] = 'tests/.coveragerc'

test_env['FLASK_ENV'] = 'TEST'

test_process = subprocess.Popen(["gunicorn", "run_server:app", '--log-level=warning', '-w 1', '-b {host}:{port}'.format(host='127.0.0.1',port=port())],
                                env=test_env)

And at the conclusion of my tests I do ...

    coverage.save()
    coverage.combine()
    percent_covered = coverage.html_report(directory='covhtml')
    print "Percent Covered: {}".format(percent_covered)
    coverage.stop()

But alas .. the coverage reports does not seem to be be combining

Note: Before combine is called if I ls -alt the directory I see a listing like so ...

-rw-r--r-- .coverage.Jonathans-MacBook-Pro-3.local.49352.501916 -rw-r--r-- .coverage.Jonathans-MacBook-Pro-3.local.49352.931352

For completeness, my .coveragerc is simply: [run] parallel = True

Would love a point in the right direction -- thanks!

like image 549
Jonathan Avatar asked Oct 31 '22 23:10

Jonathan


1 Answers

Got it working -- thanks to some guidance from @NedBat.

The issue was the gunicorn was spawning off additional subprocesses -- and those where not being monitored.

To solve this I needed to leverage the site module's sitepackage.py feature. Which in total fairness was documented http://nedbatchelder.com/code/coverage/subprocess.html -- but I was just doing it wrong.

You need to create that sitepackage.py file and place it in your site-packages folder. And then any process (or subprocess) that runs in the environment will execute that file before starting the process.

like image 54
Jonathan Avatar answered Nov 09 '22 15:11

Jonathan