The next few lines are various assertions that we have used to verify the response data. And then navigating the json using its entities to find the data we want to put an assertion on. Here we are converting our response object to a JSON syntax using resp.json() method. Here we are checking if the returned response code is 200 and if not we will print a message for failed assertion. ![]() We can fetch the retrieved response code by using “resp.status_code”. The condition here is to check the response code of the GET request. Rather found : ” + str(resp.status_code)Īssert is used in PyTest format to put an assertion for a condition. Requests.get() method sends an HTTP GET request to the given URL and returns the response object which contains all response data from the GET request and saves it to the “resp” variable.Īssert (resp.status_code = 200), “Status code is not 200. This statement is the part of test_api_get() method which is one of our test cases to test the GET method of API under test. Using this statement, we are importing Python’s requests module in our project. \Įxpected : QA, but found : " + str(data) \Įxpected : John, but found : " + str(data)Īssert data = "QA", "User created with wrong job. ![]() Rather found : "\Īssert data = "John", "User created with wrong name. "Data not matched! Expected : Holt, but found : " + str(record)Īssert (resp.status_code = 201), "Status code is not 201. "Data not matched! Expected : Eve, but found : " + str(record) Please copy and paste the following code into your “test_apitest.py” file : import requestsĪssert (resp.status_code = 200), "Status code is not 200. We will be sending requests to the endpoints defined in this API. Now, for this tutorial, we will be using a sample test API available online: We will be using this module to send requests to our API and record the response. “requests” is Python’s inbuilt module which is used to send HTTP requests to a server. In “test_apitest.py” file, our first statement would be : import requests PyTest picks all those python files in your project which start with “test_” for test execution. We will be running our tests using Python’s inbuilt testing framework called PyTest. Please create a new Python file in your project. Or you can refer to this guide about how to set up your first Python project with P圜harm :Īs now you already have set up your Python project with Pycharm, let’s create a new Python file where we will add our test cases. If you are already aware of how to create Python Project with P圜harm, please move to the next section. Pre-requisitesįirst, we will need to create a new Python project with P圜harm. In this blog, we will demonstrate how you can design an automation framework for API testing using Python. API testing is critical for automating testing because APIs now serve as the primary interface to application logic and because GUI tests are difficult to maintain with the short release cycles and frequent changes commonly used with Agile software development and DevOps. Since APIs lack a GUI, API testing is performed at the message layer. ![]() In API automation, Automation, Python, QAĪPI testing involves testing the application programming interfaces (APIs) directly and as part of integration testing to determine if they meet expectations for functionality, reliability, performance, and security.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |