|
| 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 |
0 commit comments