Month 20 — Slow and steady with TDD, Pytest and Django

This is an update for the last 2 months — June and July as I could not find time to write an update last month. I and my son have been pair-programming together on a daily basis — typically 8 pomodoros, if my work schedule allows it. If not, either 2 or 4 pomodoros.

Data Quality checking with Pandas

Many little moments of joy and satisfaction from TDD

Jupyter or VSCode?

But if go to VSCode, I was missing some of the quick experimentation and the pretty printing that Jupyter provides. Printing the pandas dataframe to the console and trying to make sense of it was painful. So, over time, I have figured out that Jupyter is for experimentation and VSCode is for writing real code.

For example, there were a number of cases I did not clearly know what I wanted. Or I knew what I wanted, but I did not know enough python or pandas to get it done. Hence, I needed to experiment in Jupyter notebook first to figure out what I wanted. Then, I would come back to VSCode and start from scratch and going through the motions of TDD to spin out the code again.

This has led to a surprising behaviour in me — once I write some code, I feel some attachment to it. I don’t want to throw it away or I don’t want to scrap and rewrite it. But somehow with TDD, this attachment seems to be going away slowly. Many a times, when I have working code that I could copy and paste from Jupyter notebook to VSCode, I would just delete the whole thing in Jupyter and start from scratch in VSCode. It is such a nice feeling to see my code evolve using the red-green-refactor process. I am grateful to Harry for his wonderful book TDD with Python that took me by hand into this beautiful world.

Discovered YAML

Quran SRS Project v2

When I started reviewing the code, the first feeling I got was that it was very complex. Thank God, I used to write lot of comments and I tried my best to write code in such a way that my future self would not totally lost. But lost I was :) — there were 2 complex pieces — Django itself and the algorithm that I had created for spaced repetition. My original thought was that I could just add tests and then start fixing bugs and adding features. But after reviewing the code, I felt that it would be better to start from scratch and rebuild. Especially since many of the pieces are quite new to me — Django, TDD, pytest, etc.

I went back to Harry’s book and started going through his example page-by-page and trying to apply the ideas to my project. He used Selenium — I decided to try Cypress since I did not really like Selenium and I loved Cypress when I looked at it in a YouTube demo. So, I spent some time learning Selenium and writing some simple functional tests.

Then, we spinned the Django project, set up pytest and started writing unit tests and getting them to pass. Writing this way helps me to understand how Django works behind the scenes as I could see the impact of each line of code or each setting that I create or modify. Trying to think of the best way to write unit tests also forces me to understand the concepts deeper level.

For example, yesterday, I read in Harry’s book that we should always redirect after a POST. I wondered why — so, I went and read up about it and I was very impressed with the explanation. Then, I came back to see what redirect in Django was doing. I wanted to know whether it is the server doing the redirect or it is the browser making a second request and I got the answer. That helped to really see that there are 2 distinct steps in that redirect steps and I need to write distinct unit tests to check for both of them.

describe-style tests in Python

Just see this screenshot of the test that I wrote for the home page and test output from pytest. I could read the output like this — “home page template is available”

So, in the last 2 months, these are golden finds and I am enjoying them and I am grateful for them.

  • Cypress
  • pytest
  • pytest watch
  • pytest describe
  • Test Runner/Explorer in VSCode
  • Django native debugging in VSCode
  • Django pytest extensions

I have not yet come to the station to enjoy Django fully as a developer yet as it still seems quite complex and unwieldy. But as a user, I have been benefiting from the stability of the framework and how fast I could get it up and running, especially given that I was newbie. So, I am very grateful to Django and my Django teacher Kevin for introducing me to it.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Siraj Samsudeen

An entrepreneur who is coming back to coding after a gap of 16 years due to love of coding.