Python Forum
Pytest API Post call thrown errors
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Pytest API Post call thrown errors
#1
I am performing the API Testing and using the pytest framework. Test is failing all the time with 401 error. Couldn't figure out what was the issue and where am I going wrong. When I manually test it using Postman, it is working.

Here is the code :

import requests
import json,jsonpath
import urllib3
import constants
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)

# variables
dumpFile = "somepath"
url = "someUrl"
headers = {'Authorization' : constants.consts['siteToken']}
#siteToken = 'Bearer jwt token'

# read json input file
input_file = open("json file path", 'r')
json_input = input_file.read()
request_json = json.loads(json_input)

# make POST request with JSON Input Body
r = requests.post(url, request_json)
print(r.content)

# Verification of the response
def test_result():
    assert r.status_code == 200

# fetch header from response
print(r.headers.get("Date"))

# parse response to JSON Format
response_json = json.loads(r.text)

# validate response using Json Path
name = jsonpath.jsonpath(response_json, 'name')
print(name)
Error is as below :

test_post.py::test_result FAILED [100%]
test__post.py:35 (test_result)
404 != 200

200
404
<Click to see difference>

def test_result():
> assert r.status_code == 200
E assert 404 == 200

test__post.py:37: AssertionError


================================== FAILURES ===================================
_________________________________ test_result _________________________________

def test_result():
> assert r.status_code == 200
E assert 404 == 200

test__post.py:37: AssertionError
============================== 1 failed in 0.42s ==============================
Process finished with exit code 0

Assertion failed

Assertion failed
Reply


Messages In This Thread
Pytest API Post call thrown errors - by pyseeker - Dec-07-2019, 02:25 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Pytest Installed, but VS Code Won’t Access Pytest AstralWeeks 9 17,198 Sep-13-2023, 03:00 PM
Last Post: AstralWeeks
  Pytest mocks anthonyrmoss78 0 1,011 May-30-2023, 08:28 PM
Last Post: anthonyrmoss78
  which exception should be thrown for a sequence of the wrong length? Skaperen 1 1,635 Jan-06-2023, 04:13 AM
Last Post: deanhystad
  Pytest and rootdirectory Master_Sergius 4 7,093 Jun-01-2020, 05:05 PM
Last Post: Master_Sergius
  Invalid argument error thrown. pyseeker 4 9,724 Sep-10-2019, 07:03 PM
Last Post: pyseeker
  Pytest with requests a21250450 3 3,785 Mar-21-2019, 03:24 PM
Last Post: buran
  pytest fixture in conftest.py thrown error while in the test file runs OzzieOzzum 1 5,121 Jul-31-2018, 12:12 PM
Last Post: OzzieOzzum
  [split] Teacher (thrown in at the deep end - help) Mr90 2 3,875 May-23-2018, 02:04 PM
Last Post: DeaD_EyE
  Teacher (thrown in at the deep end - help) Mr90 5 5,078 May-22-2018, 01:08 PM
Last Post: DeaD_EyE
  pytest and caplog lazyliv 0 3,668 May-16-2018, 02:36 PM
Last Post: lazyliv

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020