Essential Python Tools #0: Pytest

This is the first in an ongoing series of essential tools that I use day to day as a python developer which I couldn’t be as productive without. First episode is on pytest. What is pytest? This should be pretty obvious by the name, it’s a testing framework for Python. Unlike the standard libraries unittest it doesn’t wholesale inherit ideas from other languages, it reads, looks and works like you’d expect in Python.

Line coverage is not branch coverage.

I’ve spent the past couple of weeks using line coverage to search out weakly tested sections of code in a large codebase I’m working on. While this has been quick at identifying areas that have room for improvement I think it’s worth highlighting that line coverage isn’t always the best indicator of quality of your tests. Take this highly contrived example… Hypothetical big bank requirement We’re a new developer and we’ve been tasked with turning a new product requirement on a mortgage service into reality.

Security and the normalisation of deviance

I’ve been involved in quite a few post mortems in my time, a running them across almost all of them was that they weren’t down to a single cause but a series of failures that combined to result in a damaging event. The most recent post mortem was a data synchronisation service that uses an unstable API. It was built by one team and inherited by another, errors have grown to be expected and it’s mostly worked.

Decorators

I love decorators. Python is a pretty concise language but all too often you can end up with cluttered code that is full of monitoring and debugging logic. Let’s take some pretty typical code and see if we can improve it with some decorators. This post uses timing as a contrived example, in the real world you’ll probably just want to using a pre-existing timing decorator and be done with it.

Varnish Debugging

I love Varnish. As far as the world of HTTP goes, in environments where you can cache it’s one of the easiest quick wins there is. However this does somewhat come at a price to debugging. Often you’ll sit back and your black box of magic doesn’t quite produce the result you were expect from it. Normally I end up adding a sleuth of headers cache status headers to help identify where things are going wrong.