When we find some open source projects on github( e.g. ruby-cucumber). We can found some green icon to display the status of the project on the README.md file. Like these:
, it looks very attractive to me. Especially the build passing status and the code coverage. Even I’m not working for the CI in our company, I’m still interested in it.
When push some code to my code base, then automatically building the code, running tests, and showing the build status & code coverage real-time on the readme page, That’s COOL! When I look into more, the travis-ci.org and the coveralls.io are free for open source project on github.
Let me have a try.
Since recently I’m learning python. First, I created a helloworldpython project on my github.
it supports most popular languages. After signing in it with github account, open the service for my helloworld project. After the authentication for travis-ci, it will add an service for the project, we can found that on the setting of the project:
simple config of travis-ci python project
Then, we should add the config file to this project, it means a .travis.yml file on the root folder in the project. As for the format of a python project, refer to the help page here, the first time I add a very simple config file for it:
language: python python: - "3.3" - "2.7" - "2.6" install: python ClassX.py script: python ClassX.py</pre>
- we are using python language for this project
- which versions of python we want to our project to compatible. I just add the 3.3, 2.7 and 2.6 there.
- the install part describes the command the install dependencies.
- the script part will include the command to run this project, you’d better run some tests here to make your project runnable.
my projects are very simple , so i just using python file.py on the install part and the script part. The part 2 can makes me very confident if my code can pass all the builds in different versions, after the three versions configured:
which commands it executed for me
The travis-ci will using different version python to build this project, we can see that failed when in python 3.3. So what’s going on there. Click the 3.1 item, we can get more details as the following:
we can found these following interesting things:
- It’s using docker container to do this build.
- Then it will clone the project with 50-depth commits. The branch name can be specified in the travis-ci webpage. I’m using master now.
- It will install python 3.3 in the container and confirm the version of python and pip.
- the last process is running the command I wrote in the .travis.yml file. And we can see why this failed.
- For each step, we can see the time costing on the right.
- Pay attention to the line 3, if we stretch it, we can see more details info of the system. This will help you to troubleshoot when somethings happens.
So, this tool is used to get code coverage info. Also easily login with github id, also free for open source project on github.
For my python project, it’s easy to find help doc here.
Basically, before running the project , three steps to got code coverage info.
First, install the python package “coverage” and add this action into the .travis.yml file’s “install” part.
Second, after build the project, using coverage to run some code or tests.
Third, in the “after_success” part of .travis.yml file, using coveralls. Done.
So, my result .travis.yml file looks like this:
language: python python: - "2.7" - "2.6" install: - python ClassX.py - pip install python-coveralls script: - coverage run ClassX.py - coverage report -m after_success: - coveralls
the line coverage report -m, after my simple use the coverage, I found the coverage report can be useful. Until now, I can see the basic coverage info in from the travis-ci’s console output like this:
And if we want to know which line are covered or not. Details in the coveralls.io looks like:
clear. Right, my next question is the number on the right of every line. The 2 means this line are executed twice ?