Skip to content

Commit 3979f0a

Browse files
adding validations at keys level
1 parent ac98e67 commit 3979f0a

File tree

6 files changed

+71
-6
lines changed

6 files changed

+71
-6
lines changed

Data/DynamicData/sample.yml

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
created_from: snap mode in pyrest framework
22
test_sample_get_request_001:
33
name: Naresh
4+
age: 20
5+
test_sample_get_request_002:
6+
name: $notnull

Library/api.py

+48-5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import json
2+
import types
13
import allure
24
import requests
35
import yaml
@@ -29,7 +31,7 @@ def verify_response_code(status_code):
2931
result = Store.current_response.status_code == status_code
3032
assert (result is True), "Response status code is not matched, \n" \
3133
"expected: " + status_code + "\n" \
32-
"actual: " + Store.current_response.status_code
34+
"actual: " + Store.current_response.status_code
3335

3436
@staticmethod
3537
def create_file_if_not_present(file_path):
@@ -58,6 +60,7 @@ def verify_response_json(file_name, key_name):
5860
allure.attach(str(Store.current_response.json()), name="Response JSON")
5961
try:
6062
expected_json = yaml_load[key_name]
63+
allure.attach(str(expected_json), name="Expected JSON")
6164
if Var.env("snap") == "1":
6265
yaml_load[key_name] = Store.current_response.json()
6366
Api.dump_in_dynamic_variable_file(file_path, yaml_load)
@@ -66,7 +69,47 @@ def verify_response_json(file_name, key_name):
6669
if Var.env("snap") == "1":
6770
yaml_load[key_name] = Store.current_response.json()
6871
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())
72+
assert (Api.json_compare(expected_json, Store.current_response.json())), \
73+
"Response doesn't match with stored json \nExpected: " + str(expected_json) + \
74+
"\nActual response: " + str(Store.current_response.json())
75+
76+
@staticmethod
77+
def ignore_keys(keys):
78+
Store.ignore_keys = keys.split(",")
79+
80+
@staticmethod
81+
def json_compare(json1, json2):
82+
ignore_keys = Store.ignore_keys
83+
allure.attach(str(ignore_keys), name="Keys Ignored while comparing")
84+
d1_filtered = dict((k, v) for k, v in json1.items() if k not in ignore_keys)
85+
d2_filtered = dict((k, v) for k, v in json2.items() if k not in ignore_keys)
86+
for k, v in d1_filtered.items():
87+
if v == "$notnull":
88+
assert (d2_filtered[k] != "Null"), "Key value " + k + " is null in response"
89+
d1_filtered[k] = d2_filtered[k]
90+
elif v == "$null":
91+
assert (d2_filtered[k] == "Null"), "Key value " + k + " is not null in response"
92+
d1_filtered[k] = d2_filtered[k]
93+
elif v == "$array":
94+
assert (type(d2_filtered[k]) in (tuple, list) is True), "Key " + k + " is not in array format"
95+
d1_filtered[k] = d2_filtered[k]
96+
elif v == "$json":
97+
try:
98+
json.loads(d2_filtered[k])
99+
result = True
100+
except ValueError as e:
101+
result = False
102+
assert (result is True), "Key " + k + " is not in json format"
103+
d1_filtered[k] = d2_filtered[k]
104+
elif v == "$boolean":
105+
result = type(d2_filtered[k]) is bool
106+
assert (result is True), "Key " + k + " is not in boolean format"
107+
d1_filtered[k] = d2_filtered[k]
108+
elif v == "$number":
109+
result = isinstance(d2_filtered[k], (int, float, complex)) and not isinstance(d2_filtered[k], bool)
110+
assert (result is True), "Key " + k + " is not in number format"
111+
d1_filtered[k] = d2_filtered[k]
112+
elif v == "$string":
113+
result = isinstance(d2_filtered[k], str)
114+
assert (result is True), "Key " + k + " is not in string format"
115+
return d1_filtered == d2_filtered

Library/store.py

+6
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,9 @@ class Store:
55
dynamic_data_path = None
66
static_data_path = None
77
current_response = None
8+
ignore_keys = []
9+
10+
@staticmethod
11+
def reset_all_variables():
12+
Store.ignore_keys = []
13+
Store.current_response = None

Tests/conftest.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ def before_each():
99
print('*-* Before each INITIALIZATION')
1010
try:
1111
yield
12-
# After each test case taking screen shots form the available drivers
1312
except Exception as e:
1413
print(e)
14+
Store.reset_all_variables()
1515
print('*-* After each END')
1616

1717

Tests/sample.py

+12
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,16 @@
1010
def test_sample_get_request_001():
1111
Api.get("/name")
1212
Api.verify_response_code(200)
13+
Api.ignore_keys("age")
1314
Api.verify_response_json("sample.yml", "test_sample_get_request_001")
15+
16+
17+
@allure.feature("Sample get request with custom params")
18+
@allure.severity('Critical')
19+
@pytest.mark.regression # Custom pytest marker to run the test cases with ease on demand
20+
@pytest.mark.plain # Custom pytest marker to run the test cases with ease on demand
21+
def test_sample_get_request_002():
22+
Api.get("/name")
23+
Api.verify_response_code(200)
24+
Api.ignore_keys("age")
25+
Api.verify_response_json("sample.yml", "test_sample_get_request_002")

pytest.ini

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ markers =
33
sanity: sanity tests marker
44
regression: regression tests marker
55
snap: Snap feature enabled for this case, should have separate file for validating the response
6+
plain: Snap feature is not recommended since the expected JSON has some custom values
67
python_files=*.py
78
python_functions=test_*
89
addopts = -rsxX

0 commit comments

Comments
 (0)