Linux Mint

How to Install Pytest on Linux Mint 21

Pytest is a popular testing framework for Python that allows developers to write test cases using a simple and intuitive syntax. It supports features such as test discovery, test parameterization, and advanced debugging and introspection. Pytest supports test parametrization, which allows developers to write a single test function that can be run with multiple sets of input data, read this guide to get it installed on Linux Mint 21.

How to Install Pytest on Linux Mint 21

To get this Pytest tool on a Linux system, you need to install Python and pip on Linux, pip is the package installer for Python. Normally Python is by default installed on almost every Linux system, but pip is not installed so to install pip execute:

$ sudo apt install python3-pip -y

Once the Python package manager pip is installed on your system then install the Pytest by just executing:

$ sudo pip3 install pytest

To see if Pytest is installed successfully check it by executing the below given command or by checking it version:

$ python3 -m pip show pytest

To uninstall Pytest use the command mentioned below:

$ sudo pip3 uninstall pytest -y

How to Run a Test File through Pytest on Linux Mint 21

If you are new to Python and to this application of Pytest then here is an example code that we have done for you which only does the increment of one in the given number and then compares it with the given answer and shows the result. Just create a .py file and paste the below given code in the file, afterwards close the file by saving it:

def func(x):

    return x + 1

def test_answer():

    assert func(5) == 6

Now to run to test this code weather it is working correctly use the Pytest tool on the Python file you created by executing:

$ pytest testfile.py

Now from the image above it can be clearly seen that the code is successfully interpreted and the condition given in the code is true, also there is a percentage shown on the right side that depicts that all the code is compiled successfully. If you just want to see the minimal information about Python code compilation then just use the -q flag and execute the below command like this:

$ pytest -q testfile.py

Conclusion

Overall, Pytest is a powerful and feature-rich testing framework that can help developers write better and more robust code in Python. It’s simple syntax, intuitive features, and wide range of built-in functionality make it a great choice for anyone looking to write tests for their Python projects. To install Pytest on a Linux system, you will need to have Python and pip installed and then using the Python package manager get it on Linux Mint 21.

About the author

Aaliyan Javaid

I am an electrical engineer and a technical blogger. My keen interest in embedded systems has led me to write and share my knowledge about them.