Skip to content

Unittest The Python builtin library for unit testing

lvittor edited this page Oct 2, 2021 · 12 revisions

Unittest: The Python builtin library for unit testing

The unittest unit testing framework was originally inspired by JUnit and has a similar flavor as major unit testing frameworks in other languages.

The four pilars of unittest

Unittest supports some important testing concepts:

  • test fixture: represents the preparation needed to perform one or more tests, and any associated cleanup actions.

  • test case: is the individual unit of testing. It checks for a specific response to a particular set of inputs.

  • test suite: is a collection of test cases, test suites, or both. It is used to aggregate tests that should be executed together.

  • test runner: is a component which orchestrates the execution of tests and provides the outcome to the user.

Creating our first iteration

As we saw in A brief introduction of the case study, we have a class called Point that will help us to represent the points of the figures that we are going to create. So let's start by writing the necessary tests that we want the Point class to implement.

First of all, we are going to create two files: test_point.py (where we define what properties we want the Point class to have) and Point.py (where we will implement those properties.

So, our project would look similar to:

src/
    __init__.py
    Point.py
tests/
    __init__.py
    test_point.py

Red step: write a test and watch it fail

Now, let's write the first tests for the properties we want from the Point class:

# test_point.py

import unittest

from src.Point import Point

class TestPoint(unittest.TestCase):

    def setUp(self):
        """Creates a Point() object with (x, y) = (3.0, 4.0)"""
        self.point = Point(3.0, 4.0)

    def test_get_x(self):
        self.assertEqual(self.point.get_x(), 3.0)

    def test_get_y(self):
        self.assertEqual(self.point.get_y(), 4.0)
# Point.py

class Point:
    pass

Then, we run the following command (note that poetry is only to handle dependencies correctly and for that reason we do not explain it in this wiki):

$ poetry run python -m unittest -v tests/test_point.py
======================================================================
ERROR: test_get_x (tests.test_point.TestPoint)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path_to_project/tests/test_point.py", line 8, in setUp
    self.point = Point(3.0, 4.0)
TypeError: Point() takes no arguments

======================================================================
ERROR: test_get_y (tests.test_point.TestPoint)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path_to_project/tests/test_point.py", line 8, in setUp
    self.point = Point(3.0, 4.0)
TypeError: Point() takes no arguments

----------------------------------------------------------------------
Ran 2 tests in 0.001s

FAILED (errors=2)

Green step: write just enough production code to get the test to pass

Now, we add the methods required to pass the tests:

# Point.py

class Point:
    def __init__(self, x, y):
        self.x = x
        self.y = y

    def get_x(self):
        return self.x
    
    def get_y(self):
        return self.y
$ poetry run python -m  unittest -v tests/test_point.py
test_get_x (tests.test_point.TestPoint) ... ok
test_get_y (tests.test_point.TestPoint) ... ok

----------------------------------------------------------------------
Ran 2 tests in 0.000s

OK

Blue step: refactor the code to fix it

Now, we refactor the code:

class Point:
    def __init__(self, x: float, y: float):
        self.x = x
        self.y = y

    def get_x(self) -> float:
        return self.x
    
    def get_y(self) -> float:
        return self.y
$ poetry run python -m  unittest -v tests/test_point.py
test_get_x (tests.test_point.TestPoint) ... ok
test_get_y (tests.test_point.TestPoint) ... ok

----------------------------------------------------------------------
Ran 2 tests in 0.000s

OK

Iterate

Congratulations! We just finished our first iteration!

For practical purposes we are not going to show all the iterations to make the code (which can be seen finished in this repository). We prefer then, to show more in detail the tools that unittest offers us as a library.

Clone this wiki locally