Skip to content

Commit d2d9252

Browse files
author
Gaurav Singh
committed
- Added a json file to store the template for a request
- Added a file_reader.py to support reading any JSON within tests/data directory - Added a tests example which gets data from JSON and makes use of pytest fixture to inject it into the test, also makes use of jsonpath-ng syntax to extract values out of the JSON array - Added jsonpath-ng to the Pipfile
1 parent d369979 commit d2d9252

File tree

5 files changed

+117
-27
lines changed

5 files changed

+117
-27
lines changed

Pipfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ requests = "*"
1010
pytest = "*"
1111
assertpy = "*"
1212
lxml = "*"
13+
jsonpath-ng = "*"
1314

1415
[requires]
1516
python_version = "3.8"

Pipfile.lock

Lines changed: 47 additions & 16 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/data/create_person.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"fname": "Sample firstname",
3+
"lname": "Sample lastname"
4+
}

tests/people_test.py

Lines changed: 46 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
1-
from json import dumps
1+
import random
2+
from json import dumps, loads
23
from uuid import uuid4
34

5+
import pytest
46
import requests
57
from assertpy.assertpy import assert_that
8+
from jsonpath_ng import parse
69

710
from config import BASE_URI
11+
from utils.file_reader import read_file
812

913

1014
def test_read_all_has_kent():
@@ -22,7 +26,7 @@ def test_read_all_has_kent():
2226

2327

2428
def test_new_person_can_be_added():
25-
unique_last_name = create_new_person()
29+
unique_last_name = create_person_with_unique_last_name()
2630

2731
# After user is created, we read all the users and then use filter expression to find if the
2832
# created user is present in the response list
@@ -32,7 +36,7 @@ def test_new_person_can_be_added():
3236

3337

3438
def test_created_person_can_be_deleted():
35-
persons_last_name = create_new_person()
39+
persons_last_name = create_person_with_unique_last_name()
3640

3741
peoples = requests.get(BASE_URI).json()
3842
newly_created_user = search_created_user_in(peoples, persons_last_name)[0]
@@ -42,14 +46,45 @@ def test_created_person_can_be_deleted():
4246
assert_that(response.status_code).is_equal_to(requests.codes.ok)
4347

4448

45-
def create_new_person():
46-
# Ensure a user with a unique last name is created everytime the test runs
47-
# Note: json.dumps() is used to convert python dict to json string
48-
unique_last_name = f'User {str(uuid4())}'
49-
payload = dumps({
50-
'fname': 'New',
51-
'lname': unique_last_name
52-
})
49+
@pytest.fixture
50+
def create_data():
51+
payload = read_file('create_person.json')
52+
53+
random_no = random.randint(0, 1000)
54+
last_name = f'Olabini{random_no}'
55+
56+
payload['lname'] = last_name
57+
yield payload
58+
59+
60+
def test_person_can_be_added_with_a_json_template(create_data):
61+
create_person_with_unique_last_name(create_data)
62+
63+
response = requests.get(BASE_URI)
64+
peoples = loads(response.text)
65+
66+
# Get all last names for any object in the root array
67+
# Here $ = root, [*] represents any element in the array
68+
# Read full syntax: https://pypi.org/project/jsonpath-ng/
69+
jsonpath_expr = parse("$.[*].lname")
70+
result = [match.value for match in jsonpath_expr.find(peoples)]
71+
72+
expected_last_name = create_data['lname']
73+
assert_that(result).contains(expected_last_name)
74+
75+
76+
def create_person_with_unique_last_name(body=None):
77+
if body is None:
78+
# Ensure a user with a unique last name is created everytime the test runs
79+
# Note: json.dumps() is used to convert python dict to json string
80+
unique_last_name = f'User {str(uuid4())}'
81+
payload = dumps({
82+
'fname': 'New',
83+
'lname': unique_last_name
84+
})
85+
else:
86+
unique_last_name = body['lname']
87+
payload = dumps(body)
5388

5489
# Setting default headers to show that the client accepts json
5590
# And will send json in the headers

utils/file_reader.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import json
2+
from pathlib import Path
3+
4+
BASE_PATH = Path.cwd().joinpath('..', 'tests', 'data')
5+
6+
7+
def read_file(file_name):
8+
path = get_file_with_json_extension(file_name)
9+
10+
with path.open(mode='r') as f:
11+
return json.load(f)
12+
13+
14+
def get_file_with_json_extension(file_name):
15+
if '.json' in file_name:
16+
path = BASE_PATH.joinpath(file_name)
17+
else:
18+
path = BASE_PATH.joinpath(f'{file_name}.json')
19+
return path

0 commit comments

Comments
 (0)