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 :
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
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
