Hi,

During development, web frameworks often spawn a subprocess to serve requests so that they can reload the code if it changes. You might be measuring only the first process, and not the spawned process that is actually doing the work.

--Ned.

On 1/22/22 10:42 AM, Skip Montanaro wrote:
I have a simple little Flask app and created a suitably simple test script, aimed at making sure I covered much of the executable code. It fires up the app with coverage enabled and hits it with a bunch of requests, then exits. When testing, I have coverage enabled:

CMD="$(which flask) run -h localhost -p $PORT"
if [ "x$DOCOVER" = "xtrue" ] ; then
    coverage run -a $CMD
else
    $CMD
fi

Coverage seems to do the right thing, creating a .coverage file on exit, and when I run the annotate, report, or html commands it does the right thing. Still, it never actually marks any statements within functions as having been executed. Here's coverage from a manual run where I poked the server with three debug endpoints:

> if FLASK_DEBUG:
>     @app.route("/env")
>     def printenv():
!         return jsonify(dict(os.environ))
 
>     @app.route('/api/help')
>     def app_help():
>         """Print available functions."""
!         func_list = {}
!         for rule in app.url_map.iter_rules():
!             if rule.endpoint != 'static':
!                 func_list[rule.rule] = str(app.view_functions[rule.endpoint])
!         return jsonify(func_list)
 
>     @app.get('/shutdown')
>     def shutdown():
!         func = request.environ.get('werkzeug.server.shutdown')
!         if func is None:
!             raise RuntimeError('Not running with the Werkzeug Server')
!         func()
!         return 'Server shutting down...\n'

Clearly, coverage noticed the action which happened during import, but despite the fact that I really did request /env, /api/help, and /shutdown, it never thought any of them were called.

Any idea what I'm missing?

Thx,

Skip Montanaro

_______________________________________________
code-quality mailing list -- code-quality@python.org
To unsubscribe send an email to code-quality-leave@python.org
https://mail.python.org/mailman3/lists/code-quality.python.org/
Member address: ned@nedbatchelder.com