All Projects → ksator → continuous-integration-with-python

ksator / continuous-integration-with-python

Licence: MIT license
How to test your python code. How to automatically run your tests for your Python code. How to get reports of the tests coverage

Programming Languages

python
139335 projects - #7 most used programming language

Projects that are alternatives of or similar to continuous-integration-with-python

cpp14-project-template
A simple, cross-platform, and continuously integrated C++14 project template
Stars: ✭ 64 (+156%)
Mutual labels:  travis-ci, continuous-integration, doctest
react-testing-mocha-chai-enzyme
A solid test setup for React components with Mocha, Chai, Sinon, Enzyme in a Webpack/Babel application.
Stars: ✭ 48 (+92%)
Mutual labels:  coveralls, travis-ci, coverage-report
containerized-golang-and-vuejs
This repository explores an application using docker, golang, and vuejs
Stars: ✭ 19 (-24%)
Mutual labels:  coveralls, travis-ci
ci-testing-python
Sample Microservice App in Python for Testing using pytest, uber/doubles, tox on CI servers like Jenkins and Travis CI using Docker + Docker-Compose for test environment.
Stars: ✭ 37 (+48%)
Mutual labels:  travis-ci, pytest
javascript-test-reporter
DEPRECATED Code Climate test reporter client for JavaScript projects
Stars: ✭ 68 (+172%)
Mutual labels:  continuous-integration, coverage-report
Greenkeeper Lockfile
🔒 Your lockfile, up to date, all the time
Stars: ✭ 181 (+624%)
Mutual labels:  travis-ci, continuous-integration
SpringBootRestAPI
A ready-to-use Template for Rest API using spring-boot-microservices, MongoDB as Database, Integrated with codecov and sonarqube, deployable to cloud.
Stars: ✭ 24 (-4%)
Mutual labels:  coveralls, travis-ci
stock reminder bot
A twitter bot that reminds you of stock and crypto predictions
Stars: ✭ 25 (+0%)
Mutual labels:  travis-ci, pytest
Tic
Tasks Integrating Continuously: CI-Agnostic Workflow Definitions
Stars: ✭ 135 (+440%)
Mutual labels:  travis-ci, continuous-integration
scikit-ci
Simpler and centralized CI configuration for Python extensions.
Stars: ✭ 15 (-40%)
Mutual labels:  travis-ci, continuous-integration
pylint-pytest
A Pylint plugin to suppress pytest-related false positives.
Stars: ✭ 2 (-92%)
Mutual labels:  pytest, pylint
travis-ci-tutorial-java
Just to learn how to use travis-ci in a java project!
Stars: ✭ 38 (+52%)
Mutual labels:  travis-ci, continuous-integration
Nevergreen
🐤 A build monitor with attitude
Stars: ✭ 170 (+580%)
Mutual labels:  travis-ci, continuous-integration
projection-pursuit
An implementation of multivariate projection pursuit regression and univariate classification
Stars: ✭ 24 (-4%)
Mutual labels:  coveralls, travis-ci
Ci Detector
Detect continuous integration environment and get information of current build
Stars: ✭ 138 (+452%)
Mutual labels:  travis-ci, continuous-integration
dashboard
Simple code build dashboard
Stars: ✭ 14 (-44%)
Mutual labels:  coveralls, travis-ci
arduino-ci-script
Bash script for continuous integration of Arduino projects
Stars: ✭ 25 (+0%)
Mutual labels:  travis-ci, continuous-integration
Bmi Calculator
React Hooks app for calculating BMI
Stars: ✭ 80 (+220%)
Mutual labels:  travis-ci, continuous-integration
Unittesting
Testing Sublime Text Packages
Stars: ✭ 95 (+280%)
Mutual labels:  travis-ci, coverage-report
docker-ci-deploy
Python script to help push Docker images to a registry using CI services
Stars: ✭ 20 (-20%)
Mutual labels:  travis-ci, continuous-integration

Build Status Coverage Status

About this repo:

  • How to syntax check python script using py_compile
  • How to run python tests using pylint
  • How to test python code with pytest.
  • How to mesure test coverage with pytest-cov (Pytest plugin for measuring coverage).
  • How to automate tests using pytest with TravisCI.
  • Automatic coverage reporting with Coveralls.

python syntax check using py_compile:

You can use the module py_compile to validate the syntax of a python script.

python -m py_compile maths.py  

python tests using pylint:

This python script uses an undefined variable

# more a.py 
a
print a

So python can't execute it.

# python a.py 
Traceback (most recent call last):
  File "a.py", line 1, in <module>
    a
NameError: name 'a' is not defined

However py_compile doesnt catch the error. py_compile only helps with syntax error and other basic checks. Python only goes into function during runtime and hence undefined variable cannot be caught using that.

# python -m py_compile a.py 

However, pylint detects this error.

pylint installation:

sudo -s
pip install astroid
pip install isort
pip install pylint

Test your script with pylint:

# pylint a.py -r no
No config file found, using default configuration
************* Module a
C:  1, 0: Missing module docstring (missing-docstring)
W:  1, 0: Statement seems to have no effect (pointless-statement)
E:  1, 0: Undefined variable 'a' (undefined-variable)
E:  2, 6: Undefined variable 'a' (undefined-variable)

Continious integration for Python code

Requirements:

Pytest

Pytest is a testing tool for python.
To test yourself your python code locally using pytest, you first need to install it:

sudo pip install pytest pytest-cov coveralls  

Actually, coveralls installation is not required locally. coveralls is used by Travis CI to push coverage report to the Coveralls service.

Travis CI:

https://travis-ci.com/
https://travis-ci.org/
You need a github account.
You can test your code from Github automatically (at each git push) using Travis CI.
Github supports webhook with Travis CI.
Travis CI runs tests every time you push to your GitHub repository.

Coveralls:

https://coveralls.io/
Travis pushes the coverage report to Coveralls every time Travis is run.
You can sign in to Travis and coveralls with your github account.

How to clone this repository:

git clone https://github.com/ksator/continuous-integration-with-python.git
cd continuous-integration-with-python/  

How does it work:

pytest:

This is a testing tool for python.

pytest installation:

pip install pytest

By default, pytest discovers tests by looking at files that have these patterns test_*.py and *_test.py
Type py.test on the command line at your project root directory: Pytest will traverse all your subdirectories, gather up all the test files, run your tests, and provide an output.

maths.py has some function definitions. test_multiple.py has the tests for math.py

doctest:

The doctest module searches for pieces of text that look like interactive Python sessions and then executes those sessions to verify that they work exactly as shown.
https://docs.python.org/2/library/doctest.html
Pytest supports doctests with the --doctest-modules flag.

maths3.py is using doctests. The tests are in the code comment.

coverage reporting:

Testing is important, measuring coverage is also important.
Pytest can measure coverage for you with the coverage plugin, found in the pytest-cov package. pytest-cov is a Pytest plugin for measuring coverage.

pytest-cov installation:

pip install pytest-cov

Use the option --cov to mesure coverage.
With the option --cov-report term-missing, we can see which lines are not covered.

Travis CI:

To make these tests and reports automatic, we use a github webhook with Travis CI.
Every time you push to your GitHub repository, Travis tests with pytest the repository.
The file .travis.yml has the Travis CI details.
The file requirements.txt has the list of python packages Travis installs.

Coveralls:

Our .travis.yml file use the service Coveralls: Travis pushes the coverage report to Coveralls every time it runs, i.e., every time you push something to your GitHub repository.

How to run python tests locally:

py.test  
py.test -v  

py.test --cov .  
py.test --cov . -v  

py.test --cov maths.py  
py.test --cov maths.py -v  

py.test --cov-report term-missing --cov .  
py.test --cov-report term-missing --cov . -v  

py.test --cov-report term-missing --cov=maths.py  
py.test --cov-report term-missing --cov=maths.py -v  

py.test --doctest-modules -v  

py.test --doctest-modules --cov .  
py.test --doctest-modules --cov . -v  

py.test --doctest-modules --cov . --cov-report term-missing  
py.test --doctest-modules --cov . --cov-report term-missing -v  

py.test --doctest-modules --cov=maths3.py  
py.test --doctest-modules --cov=maths3.py -v   

py.test --doctest-modules --cov=maths3.py --cov-report term-missing  
py.test --doctest-modules --cov=maths3.py --cov-report term-missing -v  

setup.cfg file:

You can use a setup.cfg file at the root of the project with configuration options (ignore maths2.py, cov-report term-missing, ....) to then invoke your tests with a simple call to py.test.
More details:

Looking for more help regarding these topics?

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].