Friday, April 20, 2012

Automated python unit testing, code coverage and code quality analysis with Jenkins - part 1

As a software development consultant, I do a fair amount of python development and even though python is a scripting language, I feel that it's still worthy of good coding practices, namely: unit testing, code coverage analysis and code quality analysis.  In this series of blog postings I will describe my python development environment and how I engage the tools.

In this specific blog post, I will talk about the tools I use and how to get them installed/configured.  In future postings I'll set up a sample code base with sample unit tests and run the tests manually.  Finally in the last posting, I'll talk about using Jenkins to automate the testing.

Let's get started
I tend to do most of my python development on the Linux platform.  I love python's cross platform features.  While most of my client implementations actually run in Microsoft Windows, I still do my development in Linux.

For the purposes of this blog series, I will be using Ubuntu 11.10, the 64 bit version.  This distribution/version isn't necessary, but that what I'm using.  Python 2.7 comes with the default installation.  If for whatever reason python isn't installed on your system, just type "sudo apt-get install python" from the bash prompt:

Source control
When we move on to automating the testing/analysis, you'll need to have some source control in place.  I use git.  Even if you aren't planning on doing the automation part, source control is still a good idea.  Again, you can get it with a single command: "sudo apt-get install git"


Tools of the trade
There are a number of testing, quality and coverage tools out there for python.  These are the ones I use:

Unit testing:  unittest - Comes with python.  It's part of the standard library.
Mocking:  mock by Michael Foord - It's available in apt.
Test runner: nose - Also available in apt.
Code coverage: coverage - Also, also available in apt.
Code quality: pylint -  Also, also, also available in apt.

So to get all these great tools you simply need to type one command: "sudo apt-get install python-mock python-nose python-coverage pylint"

That's it.  If you're following along, you should have a fully ready python development environment.  Tune in to the next posting where I'll create a repository, some sample code and sample unit tests.  Then I'll manually run the testing, coverage and quality analysis tools against it.