Skip to content

Commit 3aa35d4

Browse files
committed
First Commit
1 parent 204e7c5 commit 3aa35d4

15 files changed

+233
-3
lines changed

.gitignore

+17-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ parts/
2020
sdist/
2121
var/
2222
wheels/
23-
pip-wheel-metadata/
2423
share/python-wheels/
2524
*.egg-info/
2625
.installed.cfg
@@ -50,6 +49,7 @@ coverage.xml
5049
*.py,cover
5150
.hypothesis/
5251
.pytest_cache/
52+
cover/
5353

5454
# Translations
5555
*.mo
@@ -72,6 +72,7 @@ instance/
7272
docs/_build/
7373

7474
# PyBuilder
75+
.pybuilder/
7576
target/
7677

7778
# Jupyter Notebook
@@ -82,7 +83,9 @@ profile_default/
8283
ipython_config.py
8384

8485
# pyenv
85-
.python-version
86+
# For a library or package, you might want to ignore these files since the code is
87+
# intended to run in multiple environments; otherwise, check them in:
88+
# .python-version
8689

8790
# pipenv
8891
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
@@ -127,3 +130,15 @@ dmypy.json
127130

128131
# Pyre type checker
129132
.pyre/
133+
134+
# pytype static type analyzer
135+
.pytype/
136+
137+
# Cython debug symbols
138+
cython_debug/
139+
140+
.vscode
141+
142+
assets/
143+
144+
*.html

.idea/.gitignore

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/inspectionProfiles/Project_Default.xml

+12
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/inspectionProfiles/profiles_settings.xml

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/misc.xml

+4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/modules.xml

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/rest-api-requests.iml

+10
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

+19-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,19 @@
1-
# rest-api-requests-python
1+
# Testing RESTful APIs in Python with requests
2+
3+
## Run Test
4+
1. Open Terminal
5+
2. Go to project location
6+
3. Enter `pytest` and hit enter key
7+
8+
## Capture stdout
9+
1. Open Terminal
10+
2. Go to project location
11+
3. Enter `pytest -sv --capture=sys` and hit enter key
12+
13+
## Generate HTML Report using pytest-html
14+
1. Open Terminal
15+
2. Go to project location
16+
3. Enter `pytest -sv --capture=sys --html=report.html` and hit enter key
17+
18+
### HTML Report
19+
![HTML Test Report](./img/report.png?raw=true "HTML Test Report")

http-bin/test_01_post.py

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# import requests
2+
# import json
3+
#
4+
#
5+
# def test_post_status_code():
6+
#
7+
# # Endpoint or URI
8+
# url = "http://httpbin.org/post"
9+
#
10+
# # Payload or body of request
11+
# # payload = "{\r\n \"key1\": 1,\r\n \"key2\": \"value2\"\r\n}"
12+
# payload = ""
13+
#
14+
# # Headers
15+
# headers = {'Content-Type': 'text/plain'}
16+
#
17+
# # convert dict to json string by json.dumps() for body data.
18+
# response = requests.post(url, headers=headers, data=json.dumps(payload))
19+
#
20+
# # Validate response headers and body contents, e.g. status code.
21+
# assert response.status_code == 200
22+
#
23+
# response_body = response.json()
24+
# assert response_body['url'] == url
25+
#
26+
# # print response full body as text
27+
# print(response.text)

img/report.png

67.9 KB
Loading

requirements.txt

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
pytest==5.4.3
2+
requests==2.24.0
3+
responses==0.10.15
4+
pytest-html==3.1.1

test-data/test-data-zip.csv

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
us,90210,Beverly Hills
2+
it,50123,Firenze
3+
ca,Y1A,Whitehorse

tests/test_example_01.py

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import requests
2+
3+
4+
def test_get_status_code_200():
5+
response = requests.get("http://api.zippopotam.us/us/90210")
6+
assert response.status_code == 200
7+
print(response.status_code)
8+
9+
def test_get_content_type():
10+
response = requests.get("http://api.zippopotam.us/us/90210")
11+
assert response.headers["Content-Type"] == "application/json"
12+
print(response.headers["Content-Type"])
13+
14+
15+
def test_get_body_encoding_not_set_equal_to_none():
16+
response = requests.get("http://api.zippopotam.us/us/90210")
17+
response_body = response.json()
18+
assert response.encoding is None
19+
print(response.encoding)
20+
21+
22+
def test_get_body_response_with_value():
23+
response = requests.get("http://api.zippopotam.us/us/90210")
24+
response_body = response.json()
25+
assert response_body["country"] == "United States"
26+
print(response_body["country"])
27+
28+
29+
def test_get_place_name_from_body():
30+
response = requests.get("http://api.zippopotam.us/us/90210")
31+
response_body = response.json()
32+
assert response_body["places"][0]["place name"] == "Beverly Hills"
33+
print(response_body["places"][0]["place name"])
34+
35+
36+
def test_get_places_length():
37+
response = requests.get("http://api.zippopotam.us/us/90210")
38+
response_body = response.json()
39+
assert len(response_body["places"]) == 1
40+
print(len(response_body["places"]))

tests/test_example_02.py

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import pytest, requests, csv
2+
3+
4+
test_data_zip = [
5+
("us", "90210", "Beverly Hills"),
6+
("it", "50123", "Firenze"),
7+
("ca", "Y1A", "Whitehorse"),
8+
]
9+
10+
11+
@pytest.mark.parametrize("country_code, zip_code, expected_place", test_data_zip)
12+
def test_get_location_data_check_place_name(country_code, zip_code, expected_place):
13+
response = requests.get(f"http://api.zippopotam.us/{country_code}/{zip_code}")
14+
response_body = response.json()
15+
assert response_body["places"][0]["place name"] == expected_place
16+
print(response_body["places"][0]["place name"])
17+
18+
19+
def read_data_from_csv():
20+
test_data_users_from_csv = []
21+
with open("test-data/test-data-zip.csv", newline='') as csvfile:
22+
data = csv.reader(csvfile, delimiter=',')
23+
for row in data:
24+
test_data_users_from_csv.append(row)
25+
return test_data_users_from_csv
26+
27+
28+
@pytest.mark.parametrize("country_code, zip_code, expected_place", read_data_from_csv())
29+
def test_get_location_data_check_place_name_from_csv(country_code, zip_code, expected_place):
30+
response = requests.get(f"http://api.zippopotam.us/{country_code}/{zip_code}")
31+
response_body = response.json()
32+
assert response_body["places"][0]["place name"] == expected_place
33+
print(response_body["places"][0]["place name"])

tests/test_example_03.py

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import requests
2+
import uuid
3+
4+
def create_post():
5+
return {
6+
"title": "The title of my new post",
7+
"body": "A very long string containing the body of my new post",
8+
"userId": 1
9+
}
10+
11+
12+
def test_create_post():
13+
response = requests.post("https://jsonplaceholder.typicode.com/posts", json=create_post())
14+
assert response.status_code == 201
15+
print(response.status_code == 201)
16+
assert isinstance(response.json()["id"], int) is True
17+
print(isinstance(response.json()["id"], int) is True)
18+
19+
20+
def create_billpay_for(name):
21+
22+
return {
23+
"name": name,
24+
"address": {
25+
"street": "My street",
26+
"city": "My city",
27+
"state": "My state",
28+
"zipCode": "90210"
29+
},
30+
"phoneNumber": "0123456789",
31+
"accountNumber": 12345
32+
}
33+
34+
def test_post_create_billpay_using_random_name():
35+
unique_name = str(uuid.uuid4())
36+
json_object = create_billpay_for(unique_name)
37+
response = requests.post(
38+
"https://parabank.parasoft.com/parabank/services/bank/billpay?accountId=12345&amount=500",
39+
headers={"Accept": "application/json"},
40+
json=json_object
41+
)
42+
43+
assert response.status_code == 200
44+
print(response.status_code == 200)
45+
46+
assert response.json()["payeeName"] == unique_name
47+
print(response.json()["payeeName"] == unique_name)

0 commit comments

Comments
 (0)