Code Monkey home page Code Monkey logo

continuous-integration-with-python's Introduction

Build Status Coverage Status

About this repo:

  • 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.

Inpired by:

Requirements:

To test python code locally:

sudo pip install pytest pytest-cov coveralls  

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

Travis CI:

To test with Travis CI, you need a github account.
Github supports webhook with Travis CI.
Travis CI runs tests every time you push to your GitHub repository.

Coveralls:

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

Usage for this repo:

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

How does it work:

pytest:

This is a testing tool for python.
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.

pytest installation:

pip install pytest

maths.py has some function definitions. tests/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.
Travis tests with py.test the repo every time you push to your GitHub 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.

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:
https://ilovesymposia.com/2014/10/13/continuous-integration-in-python-3-set-up-your-test-configuration-files/
http://pytest.org/latest/customize.html
http://coverage.readthedocs.io/en/latest/config.html

python syntax check:

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

python -m py_compile maths.py  

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.