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.



19 comments:

  1. Hello,
    first of all i ould like to thank you for this useful tutorial.
    And i have a question please,
    I did all the steps but i can not display the cobertura report !
    The error message is "No report data"!

    Tahnk you fo answer

    ReplyDelete
  2. Replies
    1. Python, yes. Everything else varies from client to client.

      Delete
  3. Hiiii...Thanks for sharing Great info...Nice post...Keep move on...
    Python Training in Hyderabad

    ReplyDelete
  4. Usually I never comment on blogs but your article is so convincing that I never stop myself to say something about it. You’re doing a great job Man learn Python Online Course

    ReplyDelete
  5. Thanks for sharing this informative content , Great work
    Leanpitch provides online training in Scrum Master during this lockdown period everyone can use it wisely.
    CSM online

    ReplyDelete
  6. Thanks for sharing this informative content , Great work
    Leanpitch provides online training in Scrum Master during this lockdown period everyone can use it wisely.
    CSM online

    ReplyDelete
  7. Thanks for sharing this informative content , Great work
    Leanpitch provides online training in Scrum Master during this lockdown period everyone can use it wisely.
    CSM training online

    ReplyDelete
  8. Thanks for sharing this informative content , Great work
    To crack scrum master interview : Scrum Master Interview Questions

    ReplyDelete
  9. Thanks for sharing this informative content , Great work
    Leanpitch provides online training in Scrum Master during this lockdown period everyone can use it wisely.
    CSM online

    ReplyDelete
  10. Thanks for sharing this informative content , Great work
    To crack scrum master interview : Scrum Master Interview Questions

    ReplyDelete
  11. Thanks for sharing this informative content , Great work
    Leanpitch provides online training in Scrum Master during this lockdown period everyone can use it wisely.
    ICP-CAT certification

    ReplyDelete
  12. Thanks for sharing this informative content , Great work
    To crack scrum master interview : Scrum Master Interview Questions

    ReplyDelete
  13. Thanks for sharing this informative content , Great work
    To crack Agile coach interview : Agile coach Interview Questions

    ReplyDelete