This tutorial presents how to use continuous integration (CI) in a Python code development, using the pytest and Travis. Through a few simple exercises, you'll go through different features of CI.
pytest is a framework that makes building simple and scalable tests easy.
You can install it using pip (pip install -U pytest) or in a conda environment (conda install pytest).
You can run pytest within a terminal (linux and osx) or in the anaconda prompt (Windows).
In this exercise, you will write a first test in a file test_hello_world.py.
- In a file
test_hello_world.py, write a python function that prints "Hello World!" and returns 0. - In that same file, write a new function
test_hello_world()that call your hello world function and asserts that it returned 0. - Test your file running
pytest test_hello_world.py. - Questions:
- What happens if you run
pytest .? - And if you rename the file
test_hello_world.pytohello_world.py? - And if you rename the functions ?
- What happens if you run
The Fibonacci sequence is a sequence of natural numbers where each number is the sum of the two previous numbers in the sequence, starting by 0 and 1:
F[0] = 0
F[1] = 1
F[n] = F[n-1] + F[n-2]
- In a file
fibonacci.py, write a function that returns the n th Fibonacci number. - In a file
test_fibonacci.py, write a functiontest_fibonacci()that asserts the 5th Fibonacci number is a positive natural number. - Modify
test_fibonacci()totest_fibonacci(n)that checks thenth Fibonacci number, and test it for different values ofn.- You'll need to use
@pytest.mark.parametrize('n', [0, 1, 5, -1])as a decorator to your function. - What happens for a negative parameter n ?
- You'll need to use
- You now want to write a function
strict_fibonacci(n)that raises an error if parameternis not a natural number. - You can also test this function, checking that it raises an error as expected for negative
n:- You'll need to use such decorator:
@pytest.mark.parametrize('n', [1, pytest.param(-1, marks=pytest.mark.xfail(strict=True))])
- You'll need to use such decorator:
Using Travis CI, you can automatically test every commit you push on github.
How to get started with Travis (see more info in the Travis tutorial)?
- You can find Travic CI (and other Github Apps) on the Github Marketplace.
- Install and configure Travis, selecting the repository you want to test.
- Add a .travis.yml file to your repository to tell Travis CI what to do.
- Travis can test your code using different versions of Python. See this example of .travis.yml
- Testing Python under OSX is currently broken. You can't do it as for the linux example above, but you can still test an OSX Python by installing it with conda. To use any conda environment, you can also use such approach, with this .travis.yml
- Now, each time you'll push a commit on github, your full code will be tested.
- You can setup Travis in your settings to only test master branch, every branch, the pull requests, etc.
To test your code on Windows, you can use AppVeyor, which is similar to Travis.