DEV Community

Cover image for Test-Driven Development with PyTest - Part 1
Max Ong Zong Bao
Max Ong Zong Bao

Posted on • Updated on • Originally published at maxongzb.com

Test-Driven Development with PyTest - Part 1

Introduction

This will be a 3 part series for anyone who is looking to get up to speed and integrate TDD testing practices using Pytest.

When I first started to learn about TDD, it was one of my software engineering classes at University.

The professor was introducing TDD through the use of JUnit which back then I find it was a pain to set up and configure.

Plus not seeing the value of having it as a developer practice. I just chuck it away as a good to have skill but not necessarily used during software development.

Change in Perception

It was my frustration to debug my code whenever I encounter an error in my code which leads me to TDD.

I would need to constantly understand what was wrong with the code which could take a few hours for me to look for bugs in my program.

Then manually testing it before committing it to git to fix the bug in my code.

Thus I sought a better way to make my debugging live easier by incorporating TDD.

Slowly but surely I started to learn about TDD through reading books like Clean Code and Test-driven development with Python, Python Testing with PyTest.

My knowledge has grown and was further reinforced through working with other developers on code kata through JuniorDev SG's Developer Gym.

What is PyTest?

Pytest is a testing framework.

It reduces the amount of boilerplate code needed to create test cases.

Compared to the default testing framework that is bundled with Python, it is much easy to learn.

Which was called Unittest that is designed based upon JUnit testing framework in Java.

Setup and Configuration

Ok before we start creating test cases we need to set up the virtual environment & installing pytest to get started.

Setup Virtual Environment

First, we need to install the pip package called virtualenv.

I assume you are using a Linux based system or an online cloud IDE from websites like REPL.

You are encouraged to proceed if you had installed the virtualenv and created a virtual environment called venv in your terminal:

pip install virtualenv
Enter fullscreen mode Exit fullscreen mode

Now, once you had installed the virtualenv package through pip. You need to create a folder called tutorial and create a virtual environment called venv in your terminal:

mkdir tutorial
cd tutorial/
virtualenv venv
Enter fullscreen mode Exit fullscreen mode

Once you had created the virtual environment, you need to enable the newly created virtual environment by entering the following command for Linux using your terminal:

source venv/bin/activate
Enter fullscreen mode Exit fullscreen mode

Installing PyTest

Now let's install the PyTest testing framework in your virtual environment within your folder called tutorial in the terminal:

pip install pytest
Enter fullscreen mode Exit fullscreen mode

Great, once you had installed pytest, you can type the following to test if your pytest is working in the terminal:

pytest
Enter fullscreen mode Exit fullscreen mode

You should see something similar to this once PyTest is successfully installed.

pytest 1

Congrats in installing Pytest into your project. Take a chill pill and let's continue to write your first test case.

Writing Your First Test Case

Alright, welcome back!!! I will start you off by having you to create your first test case in pytest.

But first, we should create a file called test_tutorial.py:

touch test_tutorial.py
Enter fullscreen mode Exit fullscreen mode

By default, pytest recognises test files when it starts with test_something.py.

Now we shall create a test case called test_return_sum to test the return_sum function:

def test_return_sum():
    c = return_sum(1, 4)

    assert c == 5

Enter fullscreen mode Exit fullscreen mode

Now let's run the following command in your terminal.

pytest
Enter fullscreen mode Exit fullscreen mode

You will notice that the output will have a F beside test_tutorial.py.

pytest 2

The F means that a test case has failed, which is the test case called test_return_sum.

A great thing about PyTest is that it provides helpful output on how to refactor the code.

Which for this case, it is the error by just defining a return_sum function. Let us make it pass by defining return_sum function:

def return_sum(a, b):
    return a+b

Enter fullscreen mode Exit fullscreen mode

Now run the pytest command again in your terminal to see if the test case passes.

pytest 3

You will notice that in the output it will show a . instead of the previous F beside the test_tutorial.py.

The . means that the test case has passed since you had fulfilled the test requirement for it to pass.

Congrats you had first written your first test case in pytest!!!!

Congrats

Conclusion

I hope the article has been useful in getting you started to use Pytest.

The Pytest framework is actually quite extensive and we had barely scratched the surface of it.

I will start drill down on the various ways or tricks to use pytest and incorporating the use of TDD in my part 2 and 3 of this series.

If you like my article, please sign up for Max Adventurer's Newsletter for awesome content I stumble across weekly in Python, Startup and Web Development.

You can also follow me to get the latest update of my article on Dev

This post was originally posted on Max's blog at Test Driven Development with PyTest - Part 1 and Photo by Hans-Peter Gauster on Unsplash

Reference

Top comments (2)

Collapse
 
rudolfsberzins profile image
Rudolfs Berzins

Nice introductory article to Pytest. Just an observation - if you are using Python3, wouldn't it be better to use builtin venv package and not download the virtualenv?

You can get the same functionality described here with:

python3 -m venv venv

Anyway, nice article!

Collapse
 
steelwolf180 profile image
Max Ong Zong Bao • Edited

Oh, that's really nice, I didn't know about it. I'm actually used to using virtualenv so I did not explore venv.

The other KISS was to just use pipenv but I had tons of problems when I'm using it. Thus I didn't go along with it.

I just thought of using virtualenv due to it being commonly known and allows me to move on to work on the meat part which is PyTest & TDD.