Why write unit tests?

This post is the first in a series on the why, what and how of unit testing. It was originally going to be a single post but there was so much I wanted to cover it didn’t seem right to do it as a single post. For this post we’re going to look at why […]

Unit Testing with Python (Part 4) – File Operations

Previously (here, here and here) we looked at users.py and how we can create a set of tests using mocks to validate it’s working as intended. The subject we were mocking in those previous examples was file_utils.py. Here’s what file_utils.py looks like; def write_file(path, content): file_to_write = open(path, "w") file_to_write.write(content) file_to_write.close() def read_file(path): with open(path, […]

Unit Testing with Python (Part 3) – More Mocking!

Before reading this post I’d recommend reading my previous ones available here and here. In the previous tutorial we used unittest.mock to mock out our test subject’s dependencies, this simplified our tests and allowed us to focus on one piece of functionality at a time. Unfortunately the approach detailed previously has a drawback! Let’s make […]

Unit Testing with Python (Part 1) – A Simple Test

This tutorial will be the first in a small series about writing unit tests in Python, the reason it’s being split is mostly because I am learning more about it as I write! The examples below are written to Python 2.7 though I will cover 3 in the future to highlight their differences. I will […]