An introduction to unit testing with pytest
As we mentioned in the introduction, writing unit tests is an essential task in software development to deliver high-quality software. To help us be productive and efficient, a lot of libraries exist that provide tools and shortcuts dedicated to testing. In the Python standard library, a module exists for unit testing called unittest
. Even though it’s quite common in Python code bases, many Python developers tend to prefer pytest, which provides a more lightweight syntax and powerful tools for advanced use cases.
In the following examples, we’ll write a unit test for a function called add
, both with unittest
and pytest, so that you can see how they compare on a basic use case. First, we’ll install pytest:
(venv) $ pip install pytest
Now, let’s see our simple add
function, which simply performs an addition:
chapter09_introduction.py
def add(a: int, b: int) -> int: return a...