Introduction to pytest
Learn how to write tests using pytest.
We'll cover the following
Overview
To start our journey, we’ll only need a single new tool—pytest
. It is not part of Python’s standard library; however, it’s the most helpful tool for testing Python code.
pytest is a testing framework for Python that makes it easy to write and run tests. It allows you to use plain Python code and has automatic test discovery and execution in your project. It also provides useful features such as the built-in assert statement and fixtures for managing test data. It’s a popular choice for testing in the Python community because of its ease of use and flexibility.
Note: The Python standard library contains another testing library—
unittest
. However, it’s less popular because of the less intuitive API influenced by the Java testing library.
Here’s an example of a pytest
test. It creates a 100x100 image containing a 10x10 square. The function crop_image
, which can be found in the utils.py
file, crops an image based on passed coordinates (x, y)
and cropped image resolution (w, h)
.
Get hands-on with 1200+ tech skills courses.