[PYTHON] View flask coverage with pytest-cov

Introduction

In Last time, I checked the coverage of a simple function with pytest-cov. Let's check if coverage can be checked with pytest-cov in flask as well.

environment

Easy flask coverage check

In order to use pytest-cov, you need a test target and a test source, so create it. If you don't understand, please see [Unit test of flask with pytest].

Source to be tested

The source to be tested changes to see you when the acquired message is bye, and returns the message as it is at other times.

flask_mod.py



from flask import Flask, jsonify
import datetime

app = Flask(__name__)

@app.route('/greeting/<message>')
def sample(message):
    if message == 'bye':
        message = 'see you'
    return message

Source that describes the test method

I have created two test sources with message bye and other patterns. If the header is correct, the b (binary) of the answer b'see you'is not necessary.

pytest_flask_fixture.py



import pytest
from flask_mod import app

@pytest.fixture
def client():
    app.config['TESTING'] = True
    test_client = app.test_client()
    yield test_client
    test_client.delete()

def test_greeting_bye(client):
    result = client.get('/greeting/bye')
    assert b'see you' == result.data

def test_greeting_hello(client):
    result = client.get('/greeting/hello')
    assert b'hello' == result.data

Run unit tests

Now that you have the source for the test target and test method, run it.

# pytest --cov --cov-branch -v pytest_flask_fixture.py

~~~~ Abbreviation ~~~~~

collected 2 items

pytest_flask_fixture.py ..                           [100%]

----------- coverage: platform win32, python 3.6.5-final-0 -----------
Name                          Stmts   Miss Branch BrPart  Cover
---------------------------------------------------------------
flask_mod.py                  7      0      2      0   100%
pytest_flask_fixture.py      13      0      0      0   100%
---------------------------------------------------------------
TOTAL                            20      0      2      0   100%


====== 2 passed in 0.74s ======

Looking at the results, you can see that the flask can also be tested because the Branch of `flask_mod.py` that wrote the processing of flask is 2 and the Cover is 100%.

How to run in VS Code

You can type pytest with a command, but there is also a method that can be executed with VS Code, so I will summarize it.

procedure

Definition creation procedure

  1. Press Ctrl + Shift + p to open the command palette
  2. Select python: Configure Tests
  3. Select pythest
  4. Select the test source directory (.Root Directory if the test source directory is root)
  5. Create `pytest.ini` in the test source directory. (Name is fixed)

pytest.ini



[pytest]
junit_family = legacy
addopts = -ra -q --cov --cov-branch

Execution procedure

  1. Press Ctrl + Shift + p to open the command palette
  2. python: Run All Tests
  3. python: Show Test Output

Precautions in VsCode

It seems that the file name of the test source needs to start with test, such as testXXXX.

in conclusion

I was able to confirm that pytest-cov is also possible with flask. WEB services are subject to drastic changes and corrections, and there are APIs and WEB pages for the target, so poor corrections may affect other locations. For that reason, test automation is improved, but at the same time, if coverage is checked, I think that more accurate impact confirmation tests can be performed.

Recommended Posts

View flask coverage with pytest-cov
Check python coverage with pytest-cov
IP restrictions with Flask
View PDF with fbterm
Hello world with flask
Programming with Python Flask
Deploy Flask with ZEIT Now
Touch Flask + run with Heroku
Unit test flask with pytest
API with Flask + uWSGI + Nginx
SNS Flask (Ajax) made with Flask
Web application development with Flask
View Python communication with Fiddler
Web application with Python + Flask ② ③
File upload with Flask + jQuery
Web application with Python + Flask ④
SNS Flask (Model) edition made with Flask
[LINE login] Verify state with Flask
When coverage fails with _sqlite3 error
View photos taken with LYTRO ILLUM
SNS Python basics made with Flask
View pytest coverage results from CircleCI
Measure Django application coverage with Coverage.py
[Memo] Links for developing with Flask
Creating a Flask server with Docker
Run the app with Flask + Heroku
Persist Flask API server with forever
[Python] Use Basic/Digest authentication with Flask
Basic authentication and Digest authentication with Flask
Creating a simple app with flask
Build Flask environment with Dockerfile + docker-compose.yml
SNS made with Flask Flask (Blueprint, bcrypt)
Post bulletin board creation with flask
Application development with Docker + Python + Flask
Image upload function with Vue.js + Flask