Automate python testing with CircleCI

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

tool

Register an account and link CircleCI and Github.

Environmental setting

Creating a project

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.

Preparing files for CircleCI

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

CircleCI settings

On the CircleCI screen, select Projects-> Repository-> Start Building-> Add Manually-> Start Building And select

スクリーンショット 2020-06-15 19.08.41.png

スクリーンショット 2020-06-15 19.10.38.png

The test results are displayed on the Pipelines screen. スクリーンショット 2020-06-15 19.12.43.png

Enable Github Status Checks

Select Settings-> Branches tab-> Add rule in the Github repository

スクリーンショット 2020-06-15 19.19.16.png

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. スクリーンショット 2020-06-15 19.21.23.png

Cut the working branch appropriately and send a pull request to develop. Then, the following status check is performed on the pull request screen. スクリーンショット 2020-06-15 19.31.42.png

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! スクリーンショット 2020-06-15 19.34.27.png

Recommended Posts

Automate python testing with CircleCI
Automate sushi making with Python
Automate UI testing with Selenium API | Crawling websites with python
Automate smartphone app testing with Appium-Python
Automate simple tasks with Python Part0
Coexistence of Python2 and 3 with CircleCI (1.0)
Automate Windows Application Testing with Windows Application Driver-Python
[Python] Automate Pelican builds with Travis CI
Statistics with python
Python with Go
Automate simple tasks with Python Part1 Scraping
Twilio with Python
Integrate with Python
Play with 2016-Python
AES256 with python
Tested with Python
python starts with ()
with syntax (Python)
Bingo with python
Zundokokiyoshi with python
Automate Facebook App Testing with Facebook Test Users
Excel with Python
Microcomputer with Python
Cast with python
AtCoder: Python: Automate sample testing as much as possible.
I tried to automate sushi making with python
What are you using when testing with Python?
Zip, unzip with python
Django 1.11 started with Python3.6
Primality test with Python
Python with eclipse + PyDev.
Scraping with Python (preparation)
Try scraping with Python.
Learning Python with ChemTHEATER 03
Sequential search with Python
"Object-oriented" learning with python
Run Python with VBA
Handling yaml with python
Serial communication with python
Learning Python with ChemTHEATER 05-1
Learn Python with ChemTHEATER
Run prepDE.py with python3
1.1 Getting Started with Python
Collecting tweets with Python
Binarization with OpenCV / Python
3. 3. AI programming with Python
Kernel Method with Python
Non-blocking with Python + uWSGI
Scraping with Python + PhantomJS
Posting tweets with python
Drive WebDriver with python
Use mecab with Python3
[Python] Redirect with CGIHTTPServer
Voice analysis with python
Think yaml with python
Operate Kinesis with Python
Getting Started with Python
Use DynamoDB with Python
Zundko getter with python
Handle Excel with python
Ohm's Law with Python