Posts tagged python

Getting Started with Python on Heroku/Cedar

Tutorial on how to create and deploy a simple Python web app using Flask, virtualenv and the Heroku Toolbelt

Lettuce introduction

How to get started with Lettuce, a BDD tool for Python based on Cucumber

Installing Selenium

melanie@mipu:~$ sudo pip install selenium 
melanie@mipu:~$ python 
Python 2.7.2+ (default, Oct 4 2011, 20:03:08) 
[GCC 4.6.1] on linux2 
Type "help", "copyright", "credits" or "license" for more information. 
>>> from selenium import webdriver 
>>> driver = webdriver.Firefox() 
>>> driver.get("http://www.svtplay.se") 
>>> assert "SVT Play" in driver.title

Installing pymetrics

melanie@mipu:~$ sudo apt-get install pymetrics
melanie@mipu:~/pyProject$ find -iname "*.py" | xargs pymetrics -q --nosql
melanie@mipu:~/pyProject$ ls
metricData.csv  src  tests

Jenkins and Pylint

Install pylint:

For some reason, installing pylint via pip crashed, however installing it using apt-get worked fine

melanie@mipu:~$ sudo apt-get install pylint

Running pylint:

melanie@mipu:~/pyProject$ find -iname "*.py" | xargs pylint -f parseable > pylint.txt
No config file found, using default configuration

To make pylint stop complaining about the missing config file, generate one with the following command:

melanie@mipu:~/pyProject$ pylint --generate-rcfile > .pylint

Make sure to add the file to your repository so it’s available for your Jenkins build.

Modified command for running pylint together with your config file:

melanie@mipu:~/pyProject$ find -iname "*.py" | xargs pylint --rcfile=.pylint -f parseable > pylint.txt

Jenkins build step:

First step: Add Build Step > Execute Shell
Second step: Add the following command to Execute Shell > Command

#!/bin/bash find -iname "*.py" | xargs pylint --rcfile=.pylint -f parseable > pylint.txt
echo "pylint complete"

Adding an echo as the last statement is only necessary if you want to use lint for analytical purposes only and not for deciding whether the build was successful or not. Also there’s probably a better way to find all python files e.g. by using python packages

Plot result with the violations plugin:

Install the Jenkins Violations plugin. Then back in your project add the name of the file generated by pylint to Post-builds Actions > Report Violations > pylint 

Python CI with unit test result and coverage report

Plugins:

  • Jenkins Mercurial plugin
  • xUnit Plugin
  • Jenkins Cobertura Plugin

Source Code Management:

Mercurial

  • Repository URL, e.g. /home/melanie/pyProject
  • Branch, e.g. default

Build Triggers:

  • Poll SCM > Schedule, e.g.
    15 * * * *

Build:

  • Execute shell > Command:
    nosetests --with-xcoverage --with-xunit

Post-build Actions:

  • Publish Cobertura Coverage Report > Cobertura xml report pattern:
    coverage.xml
  • Publish JUnit test result report > Test report XMLs:
    nosetests.xml

Nose: Generating XML reports

Cobertura-style coverage reports:

melanie@mipu:~$ sudo pip install nosexcover
melanie@mipu:~/pyProject$ nosetests --with-xcoverage
..
Name          Stmts   Miss  Cover   Missing
-------------------------------------------
hello_world       2      0   100%   
----------------------------------------------------------------------
Ran 2 tests in 0.050s

OK
melanie@mipu:~/pyProject$ ls
coverage.xml  src  tests

Xunit report:

melanie@mipu:~/pyProject$ nosetests --with-xunit
..
Name          Stmts   Miss  Cover   Missing
-------------------------------------------
hello_world       2      0   100%   
----------------------------------------------------------------------
Ran 2 tests in 0.041s

OK
melanie@mipu:~/pyProject$ ls
nosetests.xml  src  tests

All reports:

melanie@mipu:~/pyProject$ nosetests --with-xcoverage --with-xunit
..
Name          Stmts   Miss  Cover   Missing
-------------------------------------------
hello_world       2      0   100%   
----------------------------------------------------------------------
Ran 2 tests in 0.041s

OK

melanie@mipu:~/pyProject$ ls
coverage.xml  nosetests.xml  src  tests

Installing coverage

Before coverage installation: 

melanie@mipu:~/pyProject$ nosetests --with-coverage
nose.plugins.cover: ERROR: Coverage not available: unable to import coverage module

Installing Coverage:

melanie@mipu:~$ sudo pip install coverage
melanie@mipu:~$ coverage
Code coverage for Python.  Use 'coverage help' for help.
After successful installation:
melanie@mipu:~/pyProject$ nosetests --with-coverage
..
Name          Stmts   Miss  Cover   Missing
-------------------------------------------
hello_world       2      0   100%   
----------------------------------------------------------------------
Ran 2 tests in 0.040s

OK

Nose example

melanie@mipu:~/pyProject$ tree
.

├── src

│   └── hello_world.py

└── tests

    └── src

        └── hello_world_tests.py

melanie@mipu:~/pyProject$ nosetests
.F
======================================================================
FAIL: test_with_non_empty_string (hello_world_tests.RevertTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/melanie/pyProject/tests/src/hello_world_tests.py", line 9, in test_with_non_empty_string
    self.assertEquals("cba", revert("abc"))
AssertionError: 'cba' != 'abc'
----------------------------------------------------------------------
Ran 2 tests in 0.006s

Installing nose

melanie@mipu:~$ sudo apt-get install python-pip
melanie@mipu:~$ sudo apt-get install nose
melanie@mipu:~$ nosetests
----------------------------------------------------------------------
Ran 0 tests in 0.006s
OK