The feature of CircleCI is that you can use the CI environment for free if you have one environment. When the environment construction is completed, the CI container will be launched for each commit on GitHub and the defined automated test will be executed on the container. In this post, I'm testing a web server built with Python 3.5 and Flask running on CircleCI, using requests to access the web server on the container and returning HTTP Status code 200.
Test a web server built with Python3.5 + Flask
This article is a simple test to start a web server using Flask, a Python web framework, on CircleCI, access an HTTP endpoint, and check the HTTP Status.
The jenkins craftsmanship problem is an uncommon coined word that is said within my narrow observation range. The problem is that if you put jenkins as a CI environment in PJ, jenkins craftsmen will generally occur and magic modification will proceed and it will be convenient, but when the jenkins environment is damaged even though you can only deploy with jenkins, the craftsmen I can only fix it. Is often the problem. I think that tragedy often occurs at the stage when jenkins becomes an indispensable infrastructure and it is required not to fall to the same level as the production environment. A respected senior said he didn't want to get involved in the jenkins cluster if possible.
CircleCI does not cause this problem because it ** aggregates all information about the CI environment in circle.yml **. Also, the problem of over-packing the SDK in one Jenkins does not occur because of the mechanism to generate a separate container for each test.
Create circle.yml
in the top directory of the repository, write the following and commit. The moment you commit, it will be hooked and the test will start automatically on CircleCI.
circle.yml
test:
override:
- pwd ~
- ifconfig
- echo HelloWorld
- exit 0
■ Execution result
See the official documentation here (https://circleci.com/docs/environment#python) for the versions of Python that CircleCI supports. If you commit, the test will run automatically.
circle.yml
machine:
python:
version: 3.5.0
test:
override:
- pwd ~
- ifconfig
- echo HelloWorld
- exit 0
pip is a Gem, package management system in Ruby. The standard method of CicleCI is to list the package name and version in requirements.txt
in the top directory of the repository. For more information, see the official documentation Python section (https://circleci.com/docs/language-python).
circle.yml
machine:
python:
version: 3.5.0
dependencies:
pre:
- pip install -r ./requirements.txt
test:
override:
- pwd ~
- ifconfig
- echo HelloWorld
- exit 0
requirements.txt
Flask==0.10.1
Flask-Script==2.0.5
pytz==2015.7
redis==2.10.5
requests==2.9.1
six==1.10.0
SQLAlchemy==1.0.11
gunicorn==19.4.5
pytest==2.8.5
** The reason for running it in the background is that if you don't run it in parallel, the process will stop waiting for the runserver to finish and the test will not end. ** **
In order to run the python script on CircleCI, it is necessary to grant permissions with chmod and set PYTHONPATH. Flask but manage.py is the effect of customizing with Flask-Script. Flask-Script is useful for experienced Django users. It is convenient to write a curl command in test: override:
to check if the server is running.
circle.yml
machine:
python:
version: 3.5.0
dependencies:
pre:
- chmod -R 777 ./example
- pip install -r ./requirements.txt
- export PYTHONPATH="/home/ubuntu/flask_template/example/application"
- python ./application/manage.py runserver:
background: true
- sleep 5
test:
override:
- pwd ~
- ifconfig
- echo HelloWorld
- curl http://127.0.0.1/example
- exit 0
Write a test with py.test. It's as simple as checking the HttpStatus. This time it is a sample, so the contents are the same
■ Code for py.test
test_example.py
# -*- coding: utf-8 -*-
from __future__ import absolute_import, unicode_literals
import requests
def test_example():
host = "127.0.0.1:5000"
url = 'http://{}/example/'.format(host)
print(url)
response = requests.get(url)
print(response.text)
assert response.status_code == 200
assert "Error" not in response.text
circle.yml
machine:
python:
version: 3.5.0
dependencies:
pre:
- chmod -R 777 ./example
- pip install -r ./requirements.txt
- export PYTHONPATH="/home/ubuntu/flask_template/example/application"
- python ./application/manage.py runserver:
background: true
- sleep 5
test:
override:
- pwd ~
- ifconfig
- echo HelloWorld
- curl http://127.0.0.1/example
- py.test ./example/tests/test_example.py
- exit 0
If you commit and the test passes, the work is completed
You can set the notification destination in Project Settings> Notifications page. Is it easy to understand if you read notify official document?
■ Notified
I haven't tried mysql, so I'd like to try it in the future. Official Document Database Settings
At first, I didn't understand the reasoning, and the learning cost was heavy, so the first step was difficult. I thought it was very good because the official documents were substantial. For the limited use of testing Python web frameworks, I felt that if someone worked hard to write a configuration file, it would be possible to mass-produce it with reference to it.