This repo contains a simple code for CI using Python.
Imagine your team is working on a simple calculator app. Your task is to write a library of basic mathematical functions: addition, subtraction, multiplication, and division. You don’t care about the actual application, because that’s what your peers will be developing, using functions from your library.
Thanks to RealPython for an amazing tutorial on Continous Integration.
Here's the link to the tutorial: https://realpython.com/python-continuous-integration/
If you want to follow this step by step, here's how you can do so:
- Create a Repo
- Set Up a Working Environment
python3 -m venv calculator- Write a Simple Python Example in file calculator.py
def add(first_term, second_term):
return first_term + second_term
def subtract(first_term, second_term):
return first_term - second_term
def multiply(first_term, second_term):
return first_term * second_term- Commit as "Add functions for addition and subtraction"
- Writing Unit tests
pip install flake8 pytest pytest-cov pip freeze > requirements.txt - To run your linter, execute the following:
flake8 --statistics
- Create a file called test_calculator.py and write following lines:
class TestCalculator: def test_addition(self): assert 4 == calculator.add(2, 2) def test_subtraction(self): assert 2 == calculator.subtract(4, 2) def test_multiplication(self): assert 100 == calculator.multiply(10, 10)
- The following command runs your test:
pytest -v --cov
- Now you have to Connect to Circle. A .yml file uses a data serialization language, YAML, and it has its own specification. The goal of YAML is to be human readable and to work well with modern programming languages for common, everyday tasks. In a YAML file, there are three basic ways to represent data:
-
- Mappings (key-value pairs)
-
- Sequences (lists)
-
- Scalars (strings or numbers)
-
- It is very simple to read:
-
Indentation may be used for structure. Colons separate key-value pairs. Dashes are used to create lists. Make a .circleci and a config.yml file with the following content:
version: 2 jobs: build: docker: - image: circleci/python:3.7 working_directory: ~/repo steps: # Step 1: obtain repo from GitHub - checkout # Step 2: create virtual env and install dependencies - run: name: install dependencies command: | python3 -m venv venv . venv/bin/activate pip install -r requirements.txt # Step 3: run linter and tests - run: name: run tests command: | . venv/bin/activate flake8 --exclude=venv* --statistics pytest -v --cov=calculator
-
- Now commit.
- Follow the tutorial in the link above for better understanding of CI.