Write our Basic Test

In this example, we first imported the Pytest module. After that we have defined a function func(x). In this function, it adds 5 to x and returns the result. Then, we defined another function named test_method. It uses an assert statement to check whether the result of func(3) is equal to 8. The purpose of this test is to verify that when you pass 3 as an argument to func, it correctly adds 5 to it, resulting in 8. If this condition is true, the test passes; otherwise, it will raise an assertion error.

test_sample.py: Passed Test Case

Python3




# importing the module
import pytest
  
#defining a function with parameter x
def func(x):
    return x+5
  
#defining an another function  
def test_method():
#check whether 3+5 = 8 or not by passing 3 as an argument in function x
    assert func(3) == 8


Run the Program

Just type the following command to run the program to run the code:

pytest test_sample.py

Output

In the below output, the argument 3 adds to 5 resulting in 8. And the condition is true. So, the test has passed.

Here is an example below , When Test Cases are failed –

Example 2: Failed Test Case

In the below code, You firstly import the “pytest” module, that provides the testing framework. After that you need to define a function func(x)’ that takes parameter “x” and returns “x +5”. You can define test function “test_method()” using the pytest naming convention. The test function uses the “assert” statement to check if the result of ‘func(3)’ is equal to 5.

Python3




# importing the module
import pytest
  
#defining a function with parameter x
def func(x):
  return x+5
  
#defining an another function 
def test_method():
 #check whether 3+5 = 5 or not by passing 3 as an argument in function x
  assert func(3) == 5


Output

Getting Started with Pytest

Python Pytest is a framework based on Python. It is mainly used to write API test cases. It helps you write better programs. In the present days of REST services, Pytest is mainly used for API testing even though we can use Pytest to write simple to complex test cases, i.e., we can write codes to test API, UI, database, etc.

In this article, we will learn about What is Pytest, the Installation of Pytest, and How you can write the test.

Table of Content

  • How to Install Pytest?
  • Write our Basic Test
  • Multiple Tests at a Time
  • What are Python Testing Frameworks?
  • Advantages of Pytest
  • Conclusion

It’s better to test your programs in Visual Studio code. Make sure Python has to be installed in your system and its extensions have to be installed in VS Code before testing your code.

Similar Reads

How to Install Pytest?

To install Pytest, you can use the package manager for your Python environment, typically either ‘pip’ or ‘condo’. Here are the general steps for installing Pytest using ‘pip’....

Write our Basic Test

...

Multiple Tests at a Time

...

What are Python Testing Frameworks?

In this example, we first imported the Pytest module. After that we have defined a function func(x). In this function, it adds 5 to x and returns the result. Then, we defined another function named test_method. It uses an assert statement to check whether the result of func(3) is equal to 8. The purpose of this test is to verify that when you pass 3 as an argument to func, it correctly adds 5 to it, resulting in 8. If this condition is true, the test passes; otherwise, it will raise an assertion error....

Advantages of Pytest

...

Conclusion

...