简体   繁体   English

AttributeError: 'dict' 对象没有属性 'url'

[英]AttributeError: 'dict' object has no attribute 'url'

I'm writing some API tests in pytest which take in a user object which is generated from a utility class.我正在 pytest 中编写一些 API 测试,它们接收从实用程序类生成的user对象。 Then it calls the requests library to make the required API calls.然后它调用requests库来进行所需的 API 调用。

Below is my code that tests the /user operations.下面是我测试/user操作的代码。

import requests

class users:
    def __init__(self, user_with_token):
        self.token = user_with_token["Token"]
        self.url = 'http://127.0.0.1:5000/api/Users/'
        self.headers = {'token': self.token}

    def get_all_users(self):
        return requests.get(self.url, headers=self.headers).status_code

    def delete_user(self):
        return requests.delete(self.url, headers=self.headers).text

    def update_user(self):
        return requests.post(self.url, headers=self.headers).text

Here are the tests that call the above:以下是调用上述内容的测试:

import pytest
from endpoints.users import users

def get_all_users(user_with_token):
    result = users.get_all_users(user_with_token)
    assert result == 200

def test_delete_all_users(user_with_token):
    result = users.delete_user(user_with_token)
    assert "Current user deleted" in result

Here is the definition that generates fake data for the test to consume:以下是生成供测试使用的假数据的定义:

def generate_user_with_token():
    fake = Faker()
    url = 'http://127.0.0.1:5000/api/Register/'

    username = fake.name().split(' ', 1)[0],
    email = fake.email(),
    password = fake.random_int(min=0, max=9999)
    token = requests.post(url, json={'Username': username,'Email Address': email,'Password': password}).text.split()[-2]

    user = {
        "Username": username,
        "Email Address": email,
        "Password": password,
        "Token": token
    }

    return user

When I run it, however.但是,当我运行它时。 I am receiving an error stating:我收到一条错误消息:

    def delete_user(self):
>       return requests.delete(self.url, headers=self.headers).text
E       AttributeError: 'dict' object has no attribute 'url'

Am I unable to use self.url ?我无法使用self.url吗? I wanted to not have to redeclare it in each defination as the endpoint is the same for each test.我不想在每个定义中重新声明它,因为每个测试的端点都是相同的。

You are calling the delete_user function on the entire class, rather than a specific instance of the class.您正在整个类上调用delete_user函数,而不是类的特定实例。 Also it is more common to make class names uppercase, so the class name would be Users rather than user .此外,使类名大写更为常见,因此类名将是Users而不是user To create an instance of the Users class, you would do users = Users(user_with_token) .要创建Users类的实例,您可以执行users = Users(user_with_token)

When you call the delete_user function on the class (rather than an instance of the class) using users.delete_user(user_with_token) , the user_with_token dict is being passed as the self parameter to the method, which is where the error is coming from.当您使用users.delete_user(user_with_token)在类(而不是类的实例)上调用delete_user函数时, user_with_token dict 将作为self参数传递给方法,这就是错误的来源。 When you instead call the function on a specific instance, Python will automatically make the self parameter be the instance.当您在特定实例上调用该函数时,Python 会自动将self参数设为实例。

When you use the class you should do something like the following (this code is assuming you have renamed your class from users to Users btw:当您使用该类时,您应该执行以下操作(此代码假设您已将类从users重命名为Users顺便说一句:

from endpoints.users import users

def get_all_users(user_with_token):
    users = Users(user_with_token)
    result = users.get_all_users()
    assert result == 200

def test_delete_all_users(user_with_token):
    users = Users(user_with_token)
    result = users.delete_user()
    assert "Current user deleted" in result

Notice that instead of users.delete_user(user_with_token) , the method is simply called with users.delete_user() , as your delete_user method is only expecting a single parameter - self , not two ( self , and user_with_token ).请注意,该方法不是users.delete_user(user_with_token) ,而是简单地使用delete_user users.delete_user()调用,因为您的delete_user方法只需要一个参数 - self ,而不是两个( selfuser_with_token )。 Once you have created an instance of the Users class, it will include the data from user_with_token so there is no need to also pass that data to the delete_user and get_all_users functions.创建Users类的实例后,它将包含来自user_with_token的数据,因此无需将该数据也传递给delete_userget_all_users函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM