Introduction to Unit Testing in Python Using unittest Framework

Daksh Gupta
The Startup
Published in
5 min readSep 25, 2019

--

Why do Unit Testing?

We can’t touch, smell, or feel the software to ascertain its quality, we need tests, and unit testing plays one of the biggest role in making sure that the software indeed does what it intends to do.

Why unittest framework?

There are multiple unit test frameworks available for python, but unittest is built-in within python distribution and we don’t need to do any pip install to use it.

This doesn’t mean that it’s better than other available frameworks like pytest, you must evaluate the available test frameworks and choose the best suited for your projects

Let’s start using unittest

To use unittest , we just need to write the following statement inside out test code

import unittest

and here is how the we can invoke the unittest fromwork to run all the unit tests from the main test file

How to write Unit Test ?

You can’t just write a test function and expect the framework to call it. You need to write your test functions inside a class which is derived from unittest.TestCase

Once you have your class which…

--

--

Daksh Gupta
The Startup

🔹Software Product Development Consultant, Trainer, & Coach🔹 Tech Speaker & Content Creator 🔹 youtube.com/@Cognitive-Programmer 🔹 linkedin.com/in/iDaksh