1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
| import requests
import pytest
import csv
from requests.exceptions import HTTPError
test_data_users = [
("1","George","Bluth","george.bluth@reres.in"),
("2","Janet","Weaver","janet.weaver@reqres.in")
]
@pytest.mark.parametrize("id,first_name,last_name, expected_email", test_data_users)
def test_using_data_object_get_user_id_check_email(id,first_name,last_name,expected_email):
try:
response = requests.get(f"https://reqres.in/api/users/{id}")
jsonResponse = response.json()
email = jsonResponse['data']['email']
assert email == expected_email
except HTTPError as http_err:
print(f'HTTP error occurred: {http_err}')
except Exception as err:
print(f'Other error occurred: {err}')
def read_test_data_from_csv():
test_data = []
filename = 'users.csv'
try:
with open(filename,newline='') as csvfile:
data = csv.reader(csvfile,delimiter=',')
next(data) # skip the header
for row in data:
test_data.append(row)
return test_data
except FileNotFoundError:
print('File not found',filename)
except Exception as e:
print(e)
@pytest.mark.parametrize("id,first_name,last_name, expected_email", read_test_data_from_csv())
def test_using_csv_get_user_id_check_email(id,first_name,last_name,expected_email):
try:
response = requests.get(f"https://reqres.in/api/users/{id}")
jsonResponse = response.json()
email = jsonResponse['data']['email']
assert email == expected_email
except HTTPError as http_err:
print(f'HTTP error occurred: {http_err}')
except Exception as err:
print(f'Other error occurred: {err}')
## To Run
# $ pytest data-driven-testing.py
|