What is CI
Continuous integration, CI (English: continuous integration), is the merging of working copies of all developers into a shared mainline several times a day. (wikipedia)
It's a mystery to hear integration, but it's basically a development method that automates build, test, and deploy tasks. By introducing CI, it seems that software development can be carried out efficiently.
In this article, we aim to build an environment where CircleCI and Github are linked so that tests are automatically executed when the python development code is pushed. Reference article: https://scotch.io/tutorials/continuous-integration-with-python-and-circle-ci
Register an account and link CircleCI and Github.
Create a project file as shown below.
Folder structure
+-- pyhton-ci
+-- src
| +-- match.py
+-- tests
+-- math-test.py
src/math.py
def addition(value1, value2):
if not isinstance(value1, int) and not isinstance(value2, int):
return 'Invalid input'
else:
return value1 + value2
tests/math-test.py
import unittest
from src.math import addition
class MathTest(unittest.TestCase):
def test_addition(self):
actual = addition(3,4)
expected = 8 #Matches the execution result
self.assertEqual(actual, expected)
Run unittest from the command line
nosetests tests/math-test.py
ʻAssertionError: 7! = 8` is output, and confirm that the test has failed.
Add the following files to your project
+-- pyhton-ci
+-- src
| +-- match.py
+-- tests
| +-- math-test.py
+-- requirements.txt
+-- .circleci
+-- config.yml
Dependency management file
requirements.txt
nose==1.3.7
circleCI configuration file
version: 2
jobs:
build:
working_directoyr: ~/python-ci
docker:
- image: circleci/python:3.6.4
steps:
- checkout
- run:
command: sudo pip install -r requirements.txt
- run:
command: nosetests tests/math-test.py
Create a new Github repository and push the entire project folder to the develop branch.
git push origin develop
On the CircleCI screen, select Projects-> Repository-> Start Building-> Add Manually-> Start Building And select
The test results are displayed on the Pipelines screen.
Select Settings-> Branches tab-> Add rule in the Github repository
Enter develop
(development branch name) in the Branch name pattern
Require status checks to pass before merging
Require branches to be up to date before merging
Check the box.
Cut the working branch appropriately and send a pull request to develop. Then, the following status check is performed on the pull request screen.
Modify the test code as below and push again.
math-test.py
class MathTest(unittest.TestCase):
def test_addition(self):
actual = addition(3,4)
expected = 7 #Matches the execution result
self.assertEqual(actual, expected)
I was able to confirm that it passed the test!
Recommended Posts