Reputation: 19
I'm writting an automated test which uses 3 files helpers.py
where I store fucntion of registering, test_mails.py
where the test is executed and it runs checking for registration for 5 mails in list, login_credentials.py
where json dictionaries are stored. test_mails.py
uses unittest library and import main sing_up
function for trying to register 5 emails through api requests. The aim of test is to check if system doesn't let these mails to pass. However, when in test_mails.py
I try to use methods from libs json
orrequesrts
with sing_up
function I get these types of errors. AttributeError: 'function' object has no attribute 'json'
. This also applies to status_code
method How to solve it?
Here are files:
helpers.py
import json
import requests
def sign_up(data):
with requests.Session() as session:
sign_up = session.post(
f'https://siteexample/api/register',
headers={
'content-type': 'application/json',
},
data=json.dumps(data)
)
print(sign_up.content)
print(sign_up.status_code)
test.mails.py
import unittest
import requests
import json
from login_credentials import data, emails_list
from helpers import *
class Mails(unittest.TestCase):
def test_sign_up_public_emails(self):
emails_dict_ls = {}
for email in emails_list:
data_copy = data.copy()
data_copy["email"] = email
emails_dict_ls.update(data_copy)
sign_up(emails_dict_ls)
if 'email' in sign_up.json() and "User with this Email already exists." in
sign_up.json()['email']:
registering = False
else:
registering = True
assert self.assertEqual(sign_up.status_code, 400)
assert sign_up.json()['status_code'] == "NOT_CONFIRMED_EMAIL"
return registering
login_credentials.py
data = {
"company": "Company",
"phone": "+1111111",
"email": "",
"password1": "defaultpassword",
"password2": "defaultpassword",
"terms_agree": True,
"first_name": "TestUser",
"last_name": "One"
}
emails_list = ['[email protected]',
'[email protected]',
'[email protected]',
'[email protected]',
'[email protected]'
]
Upvotes: 0
Views: 1086
Reputation: 466
In the sign_up
function in helpers.py. You'll have to return the response so that .json()
can be called.
Also, you can use json=data
instead of data=json.dumps(data)
since it's already a built-in function of python-requests.
def sign_up(data):
with requests.Session() as session:
response = session.post(
f'https://siteexample/api/register',
headers={
'content-type': 'application/json',
},
json=data
)
return response
In test_mails.py, You're just calling the function and not storing the value in a variable.
def test_sign_up_public_emails(self):
emails_dict_ls = {}
for email in emails_list:
data_copy = data.copy()
data_copy["email"] = email
emails_dict_ls.update(data_copy)
sign_resp = sign_up(emails_dict_ls)
if 'email' in sign_resp.json()
Upvotes: 2