Api Testing? What is that? well API testing is the process of validating that an API is working as expected. API testing can be performed manually or it can be automated.
APIs are subject to bugs and other errors, so, we need to test them to verify that APIs work correctly for the users that will use them.
The following list represents three of the most common approaches.
Integration tests are used to validate the interactions among different endpoints of the APIs
End-to-end tests are used to validate key user journeys that may involve multiple endpoints and APIs.
API load testing enables developers to confirm whether their API can operate reliably during times of peak traffic.
Install virtualenv
pip install virtualenvCreate your virtual environment
virtualenv -p path virtualenv_nameActivate your virtual environment
source virtualenv/bi/activateYou can find more information about virtualenv here
There are the basic libraries to make API testing
- requests
- pytest
We'll create three folders
- src: in this folder we can add a wrapper and the logic to consume our API
- tests: in this folder we can add all of our test
- utilities: in this folder we can add functionalities to help us with our test
A wrapper? What is that? and more important Why do I need one?, basically a wrapper will help you to retrieve data from the API, in this way we can separate the logic from our test cases.
class Core:
def get(self, url: str, headers: dict) -> Response:
return
def post(self, url: str, playload: str, headers: dict) -> Response:
return
def put(self, url: str, playload: str, headers: dict) -> Response:
return
def delete(self, url: str, headers: dict) -> Response:
returnSo, what is next?, well now we need to create functions to use the API's endpoints using Core class
class UsersEndPoints:
def __init__(self):
self.request = Core()
def login_user(self):
response = self.request.get(url='www.example.com/user/login')
return responseAnd finally, we can make our test cases.
users = UsersEndpoints()
def test_login():
response = users.login_user()
#Now you can verify the response
assert(response.status_code == 200)Logging is important for API testing, if one test fails and we don't have any logging, How we can check what is happened, we can log the data after to send it to log what we sent and what we revised as response
@staticmethod
def _print_logs(method: str, url: str, playload: str, headers: str, response: str) -> None:
#here you can handle the logsFirst, we need to create a header to send the token
class BaseHeader:
def __init__(self):
self.headers = {
'Content-Type': 'application/json',
'Accept': 'application/json',
'Authorization': 'Bearer ' + token
}Now we will use the header class
class UsersEndPoints(BaseHeader):
def __inint(self):
super().__init__()self.request.post(url='url', playload=body, headers=self.headers)Test report is a document with contains a summary of all test activities and final test result of a testing project. Based on the test report, stakkeholders can evaluate the quality of the API and make decisions.
Allure is a multi-language test report tool, Allure has active community and good documentation You can find more documentation here
Install allure on your proyect
pip install allure-pytestUsage
pytest --alluredir=/tmp/my_allure_resultsGenereted a report in your default browser
allure serve /tmp/my_allure_resultsYou can find more information here
Github: you're on Github only check my account xD