We will teach you the know-how necessary for test-driven development with Flask over a total of 5 times (which is planned and subject to change). In this article, I will introduce the minimum sample code and its execution example for automatically testing Web applications and APIs developed with Flask.
1st article 2nd Tutorial for Test Driven Development (TDD) in Flask-2 Decorators 3rd writing 4th writing 5th writing
--For those who are going to develop web applications or APIs with Flask --Those who want to study test automation
The minimum test of API created by Flask with Hello, World!
as the return value is automatically performed using the test client.
Place the sample code used in this article in the following directory structure.
flask_01/
├── Dockerfile
└── app
├── flask_app.py
└── test
└── test.py
$ docker --version
Docker version 19.03.12, build 48a66213fe
Dockerfile
Dockerfile
FROM python:3.6
USER root
RUN apt update
RUN /usr/local/bin/python -m pip install --upgrade pip
RUN pip install flask==1.1.2
COPY ./app /root/
WORKDIR /root/test
Use a simple API that outputs Hello, World!
.
flask_app.py
from flask import Flask
app = Flask(__name__)
@app.route('/hello_world')
def hello_world():
return 'Hello, World!'
if __name__ == '__main__':
app.run(host="0.0.0.0",port=5000)
Details are given in the last section.
test.py
import sys
sys.path.append('../')
#From the filename of the flask application.What erased py
import flask_app
import unittest
#Class name works in Japanese
class Test_flask_app_Normal system(unittest.TestCase):
# 1.Set common parameters
def setUp(self):
self.ENDPOINT = "http://localhost:5000/{}"
self.DATA = None
self.STATUS = "200 OK"
self.STATUS_CODE = 200
self.ROUTE = None
#Test cases work in Japanese
def test_1_hello_Being able to access the world(self):
#Set required parameters for each case
self.DATA = b"Hello, World!"
self.ROUTE = "hello_world"
# 2.Connect test client and context with with minutes
with flask_app.app.test_client() as client:
# 3.Execute API using test client
response = client.get(self.ENDPOINT.format(self.ROUTE))
# 4.Perform a test using an assert statement
assert response.data == self.DATA
assert response.status == self.STATUS
assert response.status_code == self.STATUS_CODE
return
if __name__ == '__main__':
unittest.main()
Check [Directory structure](#directory structure) and execute the following command.
$ ls
Dockerfile app
$ docker build -t pytest .
~abridgement~
$ docker run -it pytest /usr/local/bin/python /root/test/test.py
.
----------------------------------------------------------------------
Ran 1 test in 0.004s
OK
Classes that inherit from ʻunittest.TestCasealways execute
setUp (self)before executing the test case. Specifically,
setUp (self)is executed before executing
test_1_flask_app to be accessible (self). Therefore, even if there are multiple test cases, it will be executed in the same way, so it is good to initialize the common parameters used in the class. Strictly different, but you can recognize that variables initialized in
setUp (self)` can be used in common in all test cases in class.
It is possible to use the test client by using the with syntax.
The beginning of flask_app.app.test_client ()
is the base name of the python file under test with the extension removed.
This time, the test target is flask_app.py
, so it will be flask_app
.
Execute the API for flask_app.py
using the get method of the test client in
2.
The execution result is stored in the response property
.
Use an assert statement to compare the response property
with the expected result set in 1. In this test code, only three types of API return value (
response.data), status (
response.status), and status code (
response.status_code) are compared. If you want to compare it with other execution results, you can check the
response property` by using the following python function.
dir(response)
Therefore, it is advisable to select properties that need to be confirmed as appropriate and compare them with assert statements.
The test code introduced the mysterious use of Flask's test client.
Tutorial for doing Test Driven Development (TDD) in Flask--2 Decorators
Recommended Posts