Member-only story

How to Create a Simple Test Suite With Pytest

Simple yet efficient

Quentin Musy
4 min readMar 3, 2023

--

Not so long ago, I was interviewing for a company, and they gave me a code assignment. I had to provide a program, in any language, that solved a particular problem. On top of that, the program had to be tested. I chose to use Python 3 for this, as it is a language I love but don’t get to use as often as I’d like. And testing was not something I did much in Python either. Let me share how I created the test suite.

An example of testsuite results

Goal

My goal with that was also to try my hand at TDD, so I created the tests before even starting coding.

Assignment

I can’t share with you the assignment I was assigned for this particular challenge, but let’s pretend our goal is to create a simple calculator class, that does the 4 basic operations, on integers only.

Installation

Make sure you have python 3.7+ installed, then run

$ pip install -U  pytest

Make sure it worked by running

$ pytest --version
pytest 7.2.1

Architecture

We’ll follow this simple architecture

/
src/
__init__.py
tests/
__init__.py

--

--

Quentin Musy
Quentin Musy

Written by Quentin Musy

Full Stack Engineer | Writer | Musician | Knowledge is meant to be shared, not kept. That's why I write.

No responses yet