Skip to content

Commit 62f8fde

Browse files
first cut of working code with get response
1 parent f814fec commit 62f8fde

File tree

6 files changed

+89
-31
lines changed

6 files changed

+89
-31
lines changed

Data/DynamicData/sample.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
created_from: snap mode in pyrest framework
2+
test_sample_get_request_001:
3+
name: Naresh

Library/api.py

Lines changed: 65 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,72 @@
11
import allure
22
import requests
3+
import yaml
4+
import os.path
5+
36
from Library.variable import Var
47
from Library.store import Store
58

69

710
class Api:
8-
URL = None
9-
endpoint = None
10-
params = None
11-
response = None
12-
13-
def __init__(self, url):
14-
Api.URL = url
15-
16-
@classmethod
17-
def get(cls, **kwargs):
18-
response = requests.get(Api.URL + Api.endpoint, Api.params, **kwargs)
19-
allure.step("Response for Get with " + Api.endpoint)
20-
allure.step("Get request Prams " + str(Api.params))
21-
allure.step("Other get Args " + str(**kwargs))
22-
allure.attach(response.status_code, name="Response Status Code")
23-
allure.attach(response.headers, name="Response Headers")
24-
allure.attach(response.text, name="Response Text")
25-
allure.attach(response.json(), name="Response JSON")
26-
Api.response = response
27-
28-
@classmethod
29-
def set_end_point(cls, endpoint):
30-
Api.endpoint = endpoint
31-
32-
@classmethod
33-
def set_params(cls, params):
34-
Api.params = params
11+
12+
@staticmethod
13+
def get(endpoint, params=None, **kwargs):
14+
url = Var.current("URL")
15+
with allure.step("Get request with the url " + url + endpoint):
16+
response = requests.get(url + endpoint, params, **kwargs)
17+
with allure.step("Response for Get with " + endpoint + " " + str(params)):
18+
allure.attach(str(**kwargs), name="Other arguments passed for get request")
19+
allure.attach(str(response.status_code), name="Response Status Code")
20+
allure.attach(str(response.headers), name="Response Headers")
21+
allure.attach(str(response.text), name="Response Text")
22+
allure.attach(str(response.json()), name="Response JSON")
23+
Store.current_response = response
24+
return response
25+
26+
@staticmethod
27+
def verify_response_code(status_code):
28+
with allure.step("Validating the status code for the request"):
29+
result = Store.current_response.status_code == status_code
30+
assert (result is True), "Response status code is not matched, \n" \
31+
"expected: " + status_code + "\n" \
32+
"actual: " + Store.current_response.status_code
33+
34+
@staticmethod
35+
def create_file_if_not_present(file_path):
36+
if Var.env("snap") == "1":
37+
if not os.path.isfile(file_path):
38+
with open(file_path, 'w') as file:
39+
documents = yaml.dump({'created_from': 'snap mode in pyrest framework'}, file)
40+
print(documents)
41+
42+
@staticmethod
43+
def dump_in_dynamic_variable_file(file_path, params):
44+
if Var.env("snap") == "1":
45+
with open(file_path, 'w') as file:
46+
documents = yaml.dump(params, file)
47+
print(documents)
48+
49+
@staticmethod
50+
def verify_response_json(file_name, key_name):
51+
expected_json = {}
52+
file_path = Var.root_path() + "/Data/DynamicData/" + file_name
53+
Api.create_file_if_not_present(file_path)
54+
with open(file_path) as file:
55+
yaml_load = yaml.load(file, Loader=yaml.FullLoader)
56+
with allure.step("Validating the response json with stored value"):
57+
allure.attach.file(file_path, name=file_name, attachment_type=allure.attachment_type.TEXT)
58+
allure.attach(str(Store.current_response.json()), name="Response JSON")
59+
try:
60+
expected_json = yaml_load[key_name]
61+
if Var.env("snap") == "1":
62+
yaml_load[key_name] = Store.current_response.json()
63+
Api.dump_in_dynamic_variable_file(file_path, yaml_load)
64+
except Exception as e:
65+
allure.attach(str(e), name="Error while fetching the key from expected yaml file")
66+
if Var.env("snap") == "1":
67+
yaml_load[key_name] = Store.current_response.json()
68+
Api.dump_in_dynamic_variable_file(file_path, yaml_load)
69+
assert (expected_json == Store.current_response.json()), "Expected Json doesn't match with stored json" \
70+
"file \nExpected: " + str(expected_json) + "\n" \
71+
"Actual response: " \
72+
"" + str(Store.current_response.json())

Library/variable.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ def __init__(self, file_name, file_type):
3838
except Exception as e:
3939
print(e)
4040

41+
@staticmethod
42+
def root_path():
43+
return os.path.dirname(os.path.abspath(__file__)).replace("/Library", "")
44+
4145
@staticmethod
4246
def env(string):
4347
try:

Tests/conftest.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def pytest_configure(config):
2929
This hook is called for every plugin and initial conftest
3030
file after command line options have been parsed.
3131
"""
32-
print("*-* pytest_configure" + config)
32+
print("*-* pytest_configure")
3333
# Configuring the selpy with data path location
3434
Store.global_data_path = os.path.dirname(
3535
os.path.abspath(__file__)).replace("/Tests", "") + '/Data/GlobalData/global_data.yml'
@@ -48,19 +48,19 @@ def pytest_sessionstart(session):
4848
Called after the Session object has been created and
4949
before performing collection and entering the run test loop.
5050
"""
51-
print("*-* pytest_sessionstart" + session)
51+
print("*-* pytest_sessionstart")
5252

5353

5454
def pytest_sessionfinish(session, exitstatus):
5555
"""
5656
Called after whole test run finished, right before
5757
returning the exit status to the system.
5858
"""
59-
print("*-* pytest_sessionfinish" + session + exitstatus)
59+
print("*-* pytest_sessionfinish")
6060

6161

6262
def pytest_unconfigure(config):
6363
"""
6464
called before test process is exited.
6565
"""
66-
print("*-* pytest_unconfigure" + config)
66+
print("*-* pytest_unconfigure")

Tests/sample.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import allure
2+
import pytest
3+
from Library.api import Api
4+
5+
6+
@allure.feature("Sample get request")
7+
@allure.severity('Critical')
8+
@pytest.mark.regression # Custom pytest marker to run the test cases with ease on demand
9+
@pytest.mark.snap # Custom pytest marker to run the test cases with ease on demand
10+
def test_sample_get_request_001():
11+
Api.get("/name")
12+
Api.verify_response_code(200)
13+
Api.verify_response_json("sample.yml", "test_sample_get_request_001")

reports/allure/.keep

Whitespace-only changes.

0 commit comments

Comments
 (0)