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

1

7

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?

Somnifacient answered 30/10, 2019 at 17:45 Comment(0)
S
5

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.

Somnifacient answered 30/10, 2019 at 21:16 Comment(1)
Thanks. I had a similar issue with Zope.Organic

© 2022 - 2024 — McMap. All rights reserved.