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
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
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
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
Great, once you had installed pytest, you can type the following to test if your pytest is working in the terminal:
pytest
You should see something similar to this once PyTest is successfully installed.
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
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
Now let's run the following command in your terminal.
pytest
You will notice that the output will have a F beside test_tutorial.py
.
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
Now run the pytest
command again in your terminal to see if the test case passes.
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!!!!
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
Top comments (2)
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!
Oh, that's really nice, I didn't know about it. I'm actually used to using
virtualenv
so I did not explorevenv
.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 isPyTest
&TDD
.