Running flask server, nosetests and coverage Running flask server, nosetests and coverage flask flask

Running flask server, nosetests and coverage


Ok guys, 12h later I found a solution. I have checked flask, werkzeug, requests, subprocess and thread lib. To only learn that problem is somewhere else. Solution was easy in fact. The bit of code that has to modified is execution of server.py. We need to cover it as well and then merge results generated by server.py and generated by nosetests. Modified test-runner.sh looks as follows:

#!/usr/bin/env bashHOST="0.0.0.0"PORT="5001"ENVS="COVERAGE_PROCESS_START=$PWD/.apirc PYTHONPATH=$PYTHONPATH:$PWD"START_SERVER="$ENVS coverage run --rcfile=.apirc $PWD/server.py --port=$PORT --host=$HOST"eval "$START_SERVER&"eval "$ENVS nosetests -s --nologcapture --cov-config=.apirc --cov-report html --with-cov"# this is important bit, we have to stop flask server gracefully otherwise# coverage won't get a chance to collect and save all resultseval "curl -X POST http://$HOST:$PORT/0.0/shutdown/"# this will merge results from both coverage runscoverage combine  --rcfile=.apirccoverage html --rcfile=.apirc

Where .apirc in my case is looks as follows:

[run]branch = Trueparallel = Truesource = files_to_cover/[html]directory = cover

Last thing we need to do is to build into our flask, view that will allow us to greacefully shut down the server. Previously I was brute killing it with kill -9 and it used to kill not only server but coverage as well.

Follow this snippet: http://flask.pocoo.org/snippets/67/

And my view is like that:

def shutdown():    if config.SHUTDOWN_ALLOWED:        func = request.environ.get('werkzeug.server.shutdown')        if func is None:            raise RuntimeError('Not running with the Werkzeug Server')        func()        return 'Server shutting down...'

It is important to use nose-cov instead of standard coverage plugin, as it uses rcfile and allows to configure more. In our case parallel is the key, please note data_files variable is not working for nose-coverage, so you cannot override it in .apirc and you have to use default values.

After all of that, your coverage will be all but shining with valid values.

I hope it going to be helpful for someone out there.