Skip to content

Commit ae5ac93

Browse files
author
Gaurav Singh
committed
- Added test to show use of POST method to create a new person and assert if it is present in get requests
- Also added inline comments to explain the test
1 parent 09caa48 commit ae5ac93

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

tests/people_test.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,30 +8,41 @@
88

99

1010
def test_read_all_has_kent():
11+
# We use requests.get() with url to make a get request
1112
response = requests.get(BASE_URI)
13+
# response from requests has many useful properties
14+
# We can get python dict as response by using .json() method
1215
response_text = response.json()
1316
pretty_print(response_text)
1417

18+
# Also we can assert on the response status code
1519
assert_that(response.status_code).is_equal_to(200)
1620
first_names = [people['fname'] for people in response_text]
1721
assert_that(first_names).contains('Kent')
1822

1923

2024
def test_new_person_can_be_added():
25+
# Ensure a user with a unique last name is created everytime the test runs
26+
# Note: json.dumps() is used to convert python dict to json string
2127
unique_last_name = f'User {str(uuid4())}'
2228
payload = dumps({
2329
'fname': 'New',
2430
'lname': unique_last_name
2531
})
2632

33+
# Setting default headers to show that the client accepts json
34+
# And will send json in the headers
2735
headers = {
2836
'Content-Type': 'application/json',
2937
'Accept': 'application/json'
3038
}
3139

40+
# We use requests.post method with keyword params to make the request more readable
3241
response = requests.post(url=BASE_URI, data=payload, headers=headers)
3342
assert_that(response.status_code).is_equal_to(204)
3443

44+
# After user is created, we read all the users and then use filter expression to find if the
45+
# created user is present in the response list
3546
people = requests.get(BASE_URI).json()
3647
is_new_user_created = filter(lambda person: person['lname'] == unique_last_name, people)
3748
assert_that(is_new_user_created).is_true()

0 commit comments

Comments
 (0)