relaxdiego (Mark Maglana's Technical Blog)

My Development Setup (Python Edition)

Mar 9, 2014
Est. read: 3 minutes

Now that my work includes coding in Python, the first thing that I paid attention to was all the Python tools that allowed me to replicate my dev setup in Ruby. Here’s what I have so far.

TDD

Testing to me these days is a lot like seatbelts while riding a car. I do not feel safe without them. So mastering a testing framework is always top priority for me when diving into a new language. I tried nose but it appears to have lost favor from one of the open-source communities I’m in.

Below is a screenshot of a skeleton project I created that is supported by unittest, testtools, and testrepository working in concert.

Test Framework Combo

Some things to note in the above code are the following:

Some examples for using this test framework combo are as follows.

Working With the Test Framework

There are a few things going on in that screenshot so let’s break it down to the meaty parts:

Continuous Testing

Continuous testing is a very important part of a development workflow and is, I would say, at the same level of importance as continuous integration. So I was very pleased to find that this is also easily implemented in Python as it is in Ruby. For Python, I came across sniffer which supported nose out of the box but was configurable enough to run testr and friends too. So now, when I edit a file in my project, sniffer detects the change and runs testr automatically:

In Good standing

Here’s another screenshot where I deliberately fail the test:

Back to work

Here’s another screenshot showing how sniffer also automatically ran flake8 after my tests and detected a trailing whitespace:

Trailing whitespace

Right now, I’ve only configured sniffer to indiscriminately run all test instead of selectively running tests depending on which file changed. This is easily configured though by overriding a sniffer class and adding some logic there.

Monitoring Code Coverage

I found coverage which does the job just as well as Ruby’s SimpleCov:

Code coverage

Here’s the HTML report showing which lines ran and which were skipped

Code coverage repot

As with Ruby’s SimpleCov, green lines were executed while red lines were not.

Summary

I’ve gotten so used to continuous testing on my local environment that coding without it feels like driving without seatbelts. It’s really good to know that Python is just as feature rich in this regard as Ruby.