Image for post
Image for post
Photo by Chris Ried on Unsplash

Coming from a Ruby on Rails background, I really appreciate solutions that became community standards of solving problems. One of such things is DatabaseCleaner gem, which ensures that your tests run in separation and there are no data leaks between them.

Recently I was looking for a similar solution for Python, but, to my surprise, I haven’t found any. However, it’s easy to leverage what SQLAlchemy and Pytest offer to wrap tests in separate database transactions. Let me show you a neat take on the problem that I hope you will find convenient to use.

Setting up a DB connection

Before we’re able to use a database with transactions in our tests, we need to set up a separate DB instance exclusively for tests. Then, from our test suite, we need to connect to the DB. Here’s an example of how to establish a connection with a MySQL…


Your path does not need to be a conventional one. Mine was a series of lucky events combined with some hard work.

Image for post
Image for post
Photo by Hitesh Choudhary on Unsplash

The company I work for tries to mitigate the social impact of the COVID-19 pandemic. Once in a while, we have on-line coffees and lunches. During one of them, we were exchanging stories about how we started to code. When I finished telling mine, I saw my colleagues surprised by the absurdity of the turn of events. It was the time when I realized how peculiar my story is, so I decided to write it down.

The beginnings

For the majority of my school years, I had no idea what should my career path become. So many things interest me that focusing on one is nearly impossible. I always liked looking for parallels in unrelated subjects. I loved looking at diagrams comparing bones in batwing with bones in the human arm. I loved learning languages and finding common patterns between them. I loved realizing how micro- and macrocosm are similar and ruled by universal laws. …


If I knew those things when I started coding, my life as a programmer would have been much easier

Image for post
Image for post
Photo by Danial RiCaRoS on Unsplash

At the beginning of my career as a Ruby developer, I used to work on a project that had barely any tests. This, combined with poor code quality, made the development of any new feature similar to a walk through a minefield — you never knew if your change is going to crash the app, even in a different place. Add my lack of knowledge at the time and a lot of stress, and you get a nightmare of experience for any beginner programmer.

Honestly, it was a miserable time. The project left such a vivid memory that it serves as an ongoing warning to avoid similar ones at all costs. …

About

Damian Kampik

💻 Ruby, Python & a bit of JS 👨‍👩‍👧 Husband & father 🎸 Bass player 🧗‍♂️ Amateur climber | Proud member of @u2iKRK

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