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:
Once the Python package manager pip is installed on your system then install the Pytest by just executing:
To see if Pytest is installed successfully check it by executing the below given command or by checking it version:
To uninstall Pytest use the command mentioned below:
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:
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:
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:
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.