Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implementing code coverage and unit testing on existing Python/Flask app

Having trouble getting this implementation down.

What I need: Code coverage results on my existing flask application, preferably using unit tests compatible with pytest.

What I am seeing: I am seeing coverage only for lines that are executed on the startup of my app. When I use pytest or postman to send requests to my server, coverage metrics do not change. That is, I can start the server, stop it, and get the same results as starting it, sending requests, and stopping it.

I have investigated using pytest, pytest-cov, and coverage.py.

I implemented the sitecustomize.py coverage plugin workaround in site_packages to support subprocess coverage, to no effect.

I am running the coverage server like this: coverage run --source . app.py -m

Then in a new terminal, I am running pytest like this: pytest

Then I ctrl+c the server, run coverage report -m to view my output.

My app.py has contents like this:

from flask import Flask, request, render_template, make_response
from flask_cors import CORS
from flask_restplus import Resource, Api, reqparse
app = Flask(__name__)
CORS(app)
api = Api(app)

if ENV == 'dev':
    app.debug = True
else:
    app.debug = False
ns_namespace = api.namespace('namespace', description='namespace')
@ns_namespace.route("/")
class Namespace(Resource):
    def get(self):
        pass

if __name__ == '__main__':
    app.debug = True
    app.run(host='0.0.0.0')

Ideally, I want to run one command to start the app.py server, execute pytest, and record the actual code coverage. Has someone run into something like this?

like image 443
Liquidmetal Avatar asked Oct 15 '22 09:10

Liquidmetal


1 Answers

Found out the issue.

Turns out, the app.debug=True was the culprit here. app.debug spawns a separate process, or something like that, which we lose insight into coverage.

like image 74
Liquidmetal Avatar answered Oct 21 '22 04:10

Liquidmetal