Skip to content

Commit b108557

Browse files
committed
Added examples, exercises and answers for checks on XML responses
1 parent 6021a48 commit b108557

File tree

3 files changed

+127
-0
lines changed

3 files changed

+127
-0
lines changed

answers/answers_05.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import xml.etree.ElementTree as et
2+
import requests
3+
4+
5+
# Exercise 5.1
6+
# Write a test that does the following:
7+
# Perform a GET to http://parabank.parasoft.com/parabank/services/bank/accounts/12345
8+
# Parse the response into an XML ElementTree
9+
# Check that the root element name is 'account'
10+
# Check that the root element has no attributes
11+
# Check that the root element has no text
12+
def test_check_root_of_xml_response():
13+
response = requests.get("http://parabank.parasoft.com/parabank/services/bank/accounts/12345")
14+
response_body_as_xml = et.fromstring(response.content)
15+
xml_tree = et.ElementTree(response_body_as_xml)
16+
root = xml_tree.getroot()
17+
assert root.tag == "account"
18+
assert len(root.attrib) == 0
19+
assert root.text is None
20+
21+
22+
# Exercise 5.2
23+
# Write a test that does the following
24+
# Perform a GET to http://parabank.parasoft.com/parabank/services/bank/accounts/12345
25+
# Parse the response into an XML ElementTree
26+
# Find the customerId element in the tree
27+
# Check that the text of the customerId element is '12212'
28+
def test_check_specific_element_of_xml_response():
29+
response = requests.get("http://parabank.parasoft.com/parabank/services/bank/accounts/12345")
30+
response_body_as_xml = et.fromstring(response.content)
31+
xml_tree = et.ElementTree(response_body_as_xml)
32+
first_name = xml_tree.find("customerId")
33+
assert first_name.text == "12212"
34+
35+
36+
# Exercise 5.3
37+
# Write a test that does the following
38+
# Perform a GET to http://parabank.parasoft.com/parabank/services/bank/customers/12212/accounts
39+
# Parse the response into an XML ElementTree
40+
# Find all 'account' elements in the entire XML document
41+
# Check that there are more than 5 of these 'account' elements
42+
def test_check_number_of_accounts_for_12212_greater_than_five():
43+
response = requests.get("http://parabank.parasoft.com/parabank/services/bank/customers/12212/accounts")
44+
response_body_as_xml = et.fromstring(response.content)
45+
xml_tree = et.ElementTree(response_body_as_xml)
46+
accounts = xml_tree.findall(".//account")
47+
assert len(accounts) > 5
48+
49+
50+
# Exercise 5.4
51+
# Repeat Exercise 5.3, but now check that:
52+
# - at least one of the accounts is of type 'SAVINGS' (Google!)
53+
# - there is no account that has a customerId that is not equal to 12212
54+
# (Use your creativity with the last one here... There is a solution, but I couldn't
55+
# find it on Google.)
56+
def test_use_xpath_for_more_sophisticated_checks():
57+
response = requests.get("http://parabank.parasoft.com/parabank/services/bank/customers/12212/accounts")
58+
response_body_as_xml = et.fromstring(response.content)
59+
xml_tree = et.ElementTree(response_body_as_xml)
60+
savings_accounts = xml_tree.findall(".//account/type[.='SAVINGS']")
61+
assert len(savings_accounts) > 1
62+
accounts_with_incorrect_customer_id = xml_tree.findall(".//account/customerId[!.='12212']")
63+
assert len(accounts_with_incorrect_customer_id) == 0

examples/examples_05.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import xml.etree.ElementTree as et
2+
import requests
3+
4+
5+
def test_check_root_of_xml_response():
6+
response = requests.get("http://parabank.parasoft.com/parabank/services/bank/customers/12212")
7+
response_body_as_xml = et.fromstring(response.content)
8+
xml_tree = et.ElementTree(response_body_as_xml)
9+
root = xml_tree.getroot()
10+
assert root.tag == "customer"
11+
assert root.text is None
12+
13+
14+
def test_check_specific_element_of_xml_response():
15+
response = requests.get("http://parabank.parasoft.com/parabank/services/bank/customers/12212")
16+
response_body_as_xml = et.fromstring(response.content)
17+
xml_tree = et.ElementTree(response_body_as_xml)
18+
first_name = xml_tree.find("firstName")
19+
assert first_name.text == "John"
20+
assert len(first_name.attrib) == 0
21+
22+
23+
# https://docs.python.org/3/library/xml.etree.elementtree.html#elementtree-xpath
24+
def test_use_xpath_for_more_sophisticated_checks():
25+
response = requests.get("http://parabank.parasoft.com/parabank/services/bank/customers/12212")
26+
response_body_as_xml = et.fromstring(response.content)
27+
xml_tree = et.ElementTree(response_body_as_xml)
28+
address_children = xml_tree.findall(".//address/*")
29+
assert len(address_children) == 4

exercises/exercises_05.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import xml.etree.ElementTree as et
2+
import requests
3+
4+
5+
# Exercise 5.1
6+
# Write a test that does the following:
7+
# Perform a GET to http://parabank.parasoft.com/parabank/services/bank/accounts/12345
8+
# Parse the response into an XML ElementTree
9+
# Check that the root element name is 'account'
10+
# Check that the root element has no attributes
11+
# Check that the root element has no text
12+
13+
14+
# Exercise 5.2
15+
# Write a test that does the following
16+
# Perform a GET to http://parabank.parasoft.com/parabank/services/bank/accounts/12345
17+
# Parse the response into an XML ElementTree
18+
# Find the customerId element in the tree
19+
# Check that the text of the customerId element is '12212'
20+
21+
22+
# Exercise 5.3
23+
# Write a test that does the following
24+
# Perform a GET to http://parabank.parasoft.com/parabank/services/bank/customers/12212/accounts
25+
# Parse the response into an XML ElementTree
26+
# Find all 'account' elements in the entire XML document
27+
# Check that there are more than 5 of these 'account' elements
28+
29+
30+
# Exercise 5.4
31+
# Repeat Exercise 5.3, but now check that:
32+
# - at least one of the accounts is of type 'SAVINGS' (Google!)
33+
# - there is no account that has a customerId that is not equal to 12212
34+
# (Use your creativity with the last one here... There is a solution, but I couldn't
35+
# find it on Google.)

0 commit comments

Comments
 (0)