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.
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].
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
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
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%.
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.
`pytest.ini`
in the test source directory. (Name is fixed) pytest.ini
[pytest]
junit_family = legacy
addopts = -ra -q --cov --cov-branch
It seems that the file name of the test source needs to start with test, such as testXXXX.
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