Error AttributeError: 'function' object has no attribute 'json' occures when importing functions from other files

Error AttributeError: 'function' object has no attribute 'json' occures when importing functions from other files

我正在编写一个使用 3 个文件的自动化测试 helpers.py 我在其中存储注册功能,test_mails.py 在其中执行测试并运行检查列表中 5 封邮件的注册, login_credentials.py 其中 json 字典被存储。 test_mails.py 使用 unittest 库和导入主要 sing_up 函数尝试通过 api 请求注册 5 封电子邮件。测试的目的是检查系统是否不允许这些邮件通过。但是,当我在 test_mails.py 中尝试将库 jsonrequesrts 中的方法与 sing_up 函数一起使用时,我得到了这些类型的错误。 AttributeError: 'function' object has no attribute 'json'。这也适用于status_code方法 如何解决呢? 以下是文件: 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 = ['mailtest.unconfirm@yahoo.com',
               'mailtest.unconfirm@gmail.com',
               'mailtest.unconfirm@ukr.net',
               'mailtest.unconfirm@outlook.com',
               'mailtest.unconfirm@icloud.com'
               ]

在helpers.py中的sign_up函数中。您必须 return 响应才能调用 .json()

此外,您可以使用 json=data 而不是 data=json.dumps(data),因为它已经是 python-requests 的 built-in 函数。

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

在 test_mails.py 中,您只是调用函数而不是将值存储在变量中。

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()