Going from unittest to pytest for Django

Readability was the big gain

by Carl James | BLOOMINGTON, IN | Dec. 10, 2023

The website I maintain for my mother-in-law's craft business

I have been developing and maintaining small Django web apps for a few years now. I built and have supported Mme's Creations since 2020. As I have tweaked these projects over the years I have made changes to adjust to my own workflows and ideas.

Projects on Git Hub

  1. This website
  2. Mme's Creations

This weekend I made the move from using Django's unittest based built-in testing to using the third party pytest package. I will go back a bit to explain how I got to this point and decision.

Initial training

My initial self training mostly came from the Official Django Tutorial which is an excellent starting point. As I progressed I was listening the Django Chat podcast heard about co-host Will Vincent's books and I learned even more. Now in that process there were a lot of conflicting opinions on how to do things.

Why Django at all?

I'm going to go back a bit to explain how I landed on Django because it is known as an "opinionated" web framework on its own. I work in I.T., but not as a developer. I write code for my own purposes. I took a Python summer class at Indiana University Bloomington in 2015 and learned some basic computer science concepts and wrote some Python code.

Drinking from the Flask

When I decided later to take that foundation to the web, the hotness of the time was Flask so I dived in. It was really cool. I could get views rendered really fast. As I got deeper in though I was installing a ton of third party applications for basic functionality and I felt I needed a bit more formal guidance.

The Wonders of Pyramid

I went to Talk Python Training as I am a sucker for podcasts and chose to learn from Talk Python to Me podcast host Michael Kennedy. I took the fantastic Python for Entrepreneurs course to learn web development from the basic webapp to deployment. Kennedy is a big fan of Pyramid web framework with the motto "The Start Small, Finish Big Stay Finished Framework". I managed to get a side job website built in Pyramid and learned about the standards of a Python built back-end driven website.

Pivot to the boring

My side-job website didn't last long and didn't really need to be dynamic in any case. I stepped away from web for a bit and then got back into it when my mother-in-law needed a website to attend a virtual craft fair during the worst of the COVID-19 pandemic.

I needed something fast and wanted something with more batteries than flask. I had heard that Django was old, bulky, and opinionated. However the size of the community at least gave reassurance that it was actively maintained and that there would be a wealth of training and resources.

As it turns out, I found I loved Django. Yes - it is opinionated. What I find is that I generally like its opinions. The Django Admin is a very functional way to manage a database with user administration built-in. These are areas that I'm perfectly fine using pre-built tools and I like that they are part of core Django. This gives me reassurance that they are well built, tested and secure.

Testing

Following the built-in tools I tested my Django applications with Django's built-in testing tools for a couple of years. Now I have wrote Python tests with the unittest module and pytest both. I finally had some time so I decided to try pytest-django this past week and found that I really liked it. My general philosophy is to not extend my tech stack unless I have a really good reason. This is why I stuck with what came with Django for so long.

Reading vs Writing

So why do I like pytest better now? I converted the tests for both of my supported sites (including this blog). Pytest didn't reduce the number of lines significantly. The writing of the tests wasn't significantly easier than unittest. What sold me is how much easier it is to read the tests and understand what they are looking for the application to do.

Clean Agile

I recently listened to an audio book by Robert C. Martin Clean Agile: Back to Basics and he discussed how tests were often the best documentation on code for other programmers. Pytest's simpler assert statements are much easier to read than the camel-cased assert classes used by the unittest framework. The pytest-django package makes integrating into Django easy. I was able to port both in a weekend while making a few other major changes to standardize my file structure along the lines that Will Vincent recommends often.


Carl James is CompTIA A+ ce Certified

Categories || All || Coding


See a complete list of all jovian34 blogs

As always, I'd love to hear you're thoughts and questions, so feel free to shoot a Noot to @jovian34 on Mastodon and I will gladly respond and perhaps write a blog post on a topic you suggest.