Pytest and Requests for API Testing”
Overview:
In day to today life of world of software development y we can seen that APIs play a crucial role to communicating different software component . API testing play a symbolic representation to give a specific purpose in part of Software Development in order to meet a required specification ,handle error so smoothness. In Terms of Apies testing developer used HTTP (hypertext transfer protocol),response code to understand what are the basic need to understand the lifecycle working of logic and correct the basic requirements of methodology .Pytest is a most knowing Python Testing Framework that offer a more flexible way to write a code or run a view Apies test.
API Testing ?
Api(Application Programming Interface) testing is most valuable in terms of software Development that used in testing the basic functionalities ,performance , reliability, performance of application’s Apis. it involves request data to the Api and validate its response to give conformance that it meets the basic needs.
Test an APIs ,do several type of testing :
Functional testing: validate the functionality of the Api in order to handle the request and give as a expected response or output.
Performance Testing: Evaluate the high traffic and concurrent request.
Integration testing: Making Confirmation that it work with databases and third party applications.
Scenario to make best Practice of Api Testing:
- Defining Clear
- Mostly used automated tools in order to reduce risk of human errors.
- Realistic data
- watch the Api performance and response time.
Http response Code:
- 1xx informational response
- xx successful response
- xx client errors
- xx server errors
- xx server errors
status code
STATUS CODE | MEANING |
100 | Indicates that the server has received the client's request and is processing it. |
101 | server is switching protocals |
102 | Server have large request and send soon as possible. |
200 | Successful processed the code and response is send as soon as possible |
201 | indicates that the server successfully created a resource. |
204 | Means that the server processed the request but has no content to return. |
400 | Indicates that the client sent an invalid request to the server. |
401 | Means that the client is unauthorized to access the resource. |
404 | Means that the requested resource is not available on the server. |
500 | Indicates that an internal server error occurred while processing the client's request. |
503 | Means that the server is temporarily unavailable. |
102 | Means that the server is processing a large request and will send a response as soon as possible. |
Why Pyesting For Api Testing:
- Easy to use
- Highly flexible
- Efficient
- Parameterization
Api Testing Tools and Libraries:
- Requests
- Robot Framework
- Tavern
- Django Test Client
- Testify
Benefits use of Requests:
- Simple and easy-to-use interface
- Cross-platform support
- Customizable options
- Support for various authentication methods
![import request](https://thoughtcoders.com/wp-content/uploads/2024/06/code.png)
Project Setup:
- Download and install Python
- Download Requests and pytest libraries
- Download and install Visual Studio Code
- Configure pytest in Visual Studio Code
Test Scenarios:
Following are the test Scenario we will covered during pytest API testing
- Get a List of Users
- Create a New User
- Update User
- Delete User
Scenario 1: Get a list Of users :
Implementation:
![GET Request Validation with Requests - Verify Status Code](https://thoughtcoders.com/wp-content/uploads/2024/06/important-request-300x139.png)
Step1:import the requests library
Step2: create a new function ,must be start with test
Step3:set the url of endpoints.
Step4:get the reponse
Step5 we should check if the response to the request is as expected. if we want to see the result of this function.
Step6:pytest
Scenario 2: Create a new user:
Implementation:
Step1: Create a new function with a name that starts with “test_”.
Step2: Inside the test, you should set the URL of the endpoint you are going to test.
Step3: You should create a JSON payload
Step4: you need to call the suitable method from the Requests library
Step5: To finish our test, we should check if the request response is 201 as expected
Step6: see the result of the test we already created, we can just run the below command in the cmd pytest
Scenario 3: Update User
Implementation
![PUTRequest Validation with Requests - Verify Status Code](https://thoughtcoders.com/wp-content/uploads/2024/06/test-update-user.png)
Step1: Create a new function with a name that starts with “test_”.
Step2: Inside the test, you should set the URL of the endpoint you will test
Step3: You should create a JSON payload that fits what is expected by the API documentation
Step4: you need to call the suitable method from the Requests library.
Step5: we should check if the request response is 200 as expected
Step6: To see the result of the test we already created, we can just run the below command in the cmd.
Scenario 4: Delete User
Implementation:
![DELETE Request Validation with Requests - Verify Status Code](https://thoughtcoders.com/wp-content/uploads/2024/06/test-delete-user.png)
Step 1: Create a new function with a name that starts with “test_”.
Step 2: Inside the test, you should set the URL of the endpoint you will test
Step 3: You need to call the suitable method from the Requests library
Step 4: To finish our test, we should check if the request response is 204 as expected.
Step 5: To see the test result we already created, we can just run the below command in the cmd.
Hope this blogs helps you to execute API Tests using requests and pytest. Still if you have any doubt then feel free to Contact Us on info@thoughtcoders.com or call us on +919555902032.