Posted on
Software

pytest: Python testing framework

Author
  • User
    Linux Bash
    Posts by this author
    Posts by this author

Getting Started with Pytest: The Python Testing Framework on Linux

Testing is an essential part of software development that helps in ensuring the consistency, reliability, and functionality of code. pytest is one of the most popular testing frameworks in Python due to its simplicity, scalability, and ability to handle complex test scenarios. In this post, we will guide you through installing pytest on a Linux system using various package managers and introduce you to its basic usage.

What is Pytest?

pytest is a robust Python library for writing and running tests. It supports unit, functional, and integration testing. pytest makes it easy to create simple tests yet scales to support complex functional testing for applications and libraries. Some of the features of pytest include:

  • Detailed info on failing assert statements (no need to remember self.assert* names).

  • Auto-discovery of test modules and functions.

  • Modular fixtures to manage small or parametrized long-lived test resources.

  • Rich plugin architecture, with over 750+ external plugins and thriving community.

Installing Pytest

Here’s how you can install pytest on various Linux distributions:

Ubuntu and Debian-based systems:

For users on Ubuntu or Debian-based systems, pytest can be installed using apt. First, update your package list:

sudo apt update

Then install pytest:

sudo apt install python3-pytest

This command installs Python3 and pytest. You can verify the installation by checking the version of pytest:

pytest --version

Fedora and RHEL/CentOS (with dnf):

On Fedora, CentOS, or any RHEL-based distribution that uses dnf:

First, ensure that Python3 is installed:

sudo dnf install python3

Then, install pytest:

sudo dnf install python3-pytest

Check if the pytest was installed correctly:

pytest --version

openSUSE (with zypper):

For openSUSE users, zypper is the command-line interface of ZYpp package manager. To install pytest, run:

sudo zypper install python3-pytest

Verify the installation:

pytest --version

Using Python pip:

Alternatively, you can use pip, Python’s package installer. This method works regardless of your Linux distribution. It's generally a good practice to use a virtual environment but is optional. Here's how you can install pytest using pip:

python3 -m pip install --user pytest

This installs pytest locally for the current user. To install it globally (which is not recommended unless necessary), omit the --user flag:

sudo pip install pytest

Verify the installation using:

pytest --version

Writing Your First Test with pytest

After installation, creating your first test with pytest is straightforward. Here’s an example:

Create a new Python file named test_sum.py:

def test_sum():
    assert sum([1, 2, 3]) == 6, "Should be 6"

if __name__ == "__main__":
    test_sum()
    print("Everything passed")

Run the test using the pytest command:

pytest test_sum.py

pytest will look for any files prefixed with test_, execute them as tests.

Conclusion

pytest is an extremely useful framework for testing Python applications. Its ease of use and scalability make it suitable for both beginners and professionals. With this guide, you should be able to install pytest on any Linux distribution and kickstart your testing journey. Remember, good testing practices lead to more robust and error-free code. Happy testing!