How to test response time in Request using Python Requests

HOME

import requests

endpoint = 'https://reqres.in/api/users'


def test_response():
    payload = {
        "name": "Vibha",
        "Job": "CEO"
    }

    response = requests.post(endpoint, payload)
    response_body = response.json()
    print("Response :", response_body)
    assert response.status_code == 201
    print("Elapsed Time :", response.elapsed.total_seconds())

pytest ResponseTime_test.py -s

How to add header to Python Requests

HOME

Headers = { “Authorization” : ”our_unique_secret_token” }

pip install -U requests

pip install -U pytest

import requests

ENDPOINT = 'https://reqres.in/api/users'


def test_add_header():
    request_body = {
        "name": "Vibha",
        "Job": "CEO"
    }

    header = {"Content-Type":  "application/json; charset=utf-8"}

    response = requests.post(ENDPOINT, request_body, header)
    print(response.json())
    print("Request Header", response.request.headers)
    print("Response Header", response.headers)
    assert response.status_code == 201
    assert response.headers["Content-Type"] == "application/json; charset=utf-8"

assert response.headers["Content-Type"] == "application/json; charset=utf-8"

pytest PostRequest_test.py -s

How to test PATCH Request Using Python Requests

HOME

pip install -U requests

pip install -U pytest

import requests

ENDPOINT = 'https://reqres.in/api/users/2'


def test_partiallyupdate_user():
    request_body = {
        "Job": "CFO"
    }

    response = requests.patch(ENDPOINT, request_body)
    print(response.text)
    assert response.status_code == 200
    response_body = response.json()
    assert response_body["Job"] == "CFO"

How to test DELETE Request Using Python Requests

HOME

pip install -U requests

pip install -U pytest

import requests

ENDPOINT = 'https://reqres.in/api/users/2'


def test_delete_user():

    response = requests.delete(ENDPOINT)
    assert response.status_code == 204

How to test PUT Request Using Python Requests

HOME

pip install -U requests

pip install -U pytest

import requests

ENDPOINT = 'https://reqres.in/api/users/2'


def test_update_user():
    request_body = {
        "name": "Vibha",
        "Job": "CTO"
    }

    response = requests.put(ENDPOINT, request_body)
    response_body = response.json()
    print(response_body)
    assert response.status_code == 200
    assert response_body["Job"] == "CTO"

Requests in Python

HOME

How to test POST Request Using Python Requests

HOME

pip install -U requests

pip install -U pytest

import requests

ENDPOINT = 'https://reqres.in/api/users'


def test_create_user():

    response = requests.post(ENDPOINT, {"name": "Vibha", "job": "CTO"})
    response_body = response.json()
    print(response_body)
    assert response.status_code == 201
    assert response_body["name"] == "Vibha"
    assert response_body["job"] == "CTO"

def test_create_user1():
    payload = {
        "name": "Vibha",
        "job": "CTO"
    }

    response = requests.post(ENDPOINT, payload)
    response_body = response.json()
    print(response_body)
    assert response.status_code == 201
    assert response_body["name"] == "Vibha"
    assert response_body["job"] == "CTO"

How to test GET Request Using Python Requests

HOME

pip install -U requests

pip install -U pytest

import requests

ENDPOINT = 'https://reqres.in/api/'
USER = 'users/2'


def test_get_user_successful():
    response = requests.get(ENDPOINT+USER)
    response_body = response.json()
    print(response_body)
    assert response.status_code == 200
    assert response_body["data"]["id"] == 2
    assert response_body["data"]["first_name"] == "Janet"
    assert response_body["data"]["last_name"] == "Weaver"

def test_get_user_unsuccessful():
    response = requests.get(ENDPOINT+USER)
    print(response.json())
    assert response.status_code == 203