【问题标题】:Python inheritance from class/method从类/方法继承Python
【发布时间】:2020-09-29 19:48:35
【问题描述】:

我认为问题在于 selfs/class 方法等。

首先,我有这样的代码:

from locust import HttpUser, task, TaskSet, between, exception, events
import logging


alphabetic = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
              'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
              'u', 'w', 'x', 'y', 'z']

logins = [elem*3 for elem in alphabetic]


passwd = 'radio'

paths_in_dict = {"hard_path": "secure/izar.xhtml/",
                 "standard_analysis_path": "analysis/STANDARD/",
                 "smart_analysis_path": "analysis/SMART/"}

login_pass = [(elem, passwd) for elem in logins]


class MyUser(HttpUser):
    wait_time = between(5, 9)
    host = "https://localhost:9003/"

    def on_start(self):
        if len(login_pass) > 0:
            self.user, self.passwd = login_pass.pop()
        self.client.verify = False
        self.login()

    def on_stop(self):
        pass

    def login(self):
        default_headers = {'X-Username': self.user, 'X-Password': self.passwd}
        self.client.request(method="POST", url="login.xhtml", headers=default_headers,
                            name="---ON START---LOGIN")
        logging.info('Login with %s username and %s password', self.user, passwd)
    @task
    def delete_analysis(self):
        """DELETE will not be performed if Analysis does not exist"""
        default_headers = {'X-Username': self.user, 'X-Password': self.passwd}
        for number in range(101, 106):
            response = self.client.request(method="GET", url="api/analysis/%s" % number,
                                           headers=default_headers, catch_response=True,
                                           name="Check if Analysis %s exists" % number)
            if response.ok and number not in MyUser.deleted_number_analysis:
                MyUser.deleted_number_analysis[number] = True
                self.client.request(method="DELETE", url="api/analysis/%s" % number,
                                    headers=default_headers,
                                    name="Delete existing Analysis with ID %s" % number)
            else:
                response.failure(exc="Nothing to DELETE")

接下来的@tasks/methods 正在等待default_headers

所以我的问题是 - 如何为整个 claas MyUser(HttpUser) 制作 default headers

我认为我在自我、方法等方面犯了一些错误。

那么如何为这个类中的所有方法声明default_headers??

你能帮帮我吗?

【问题讨论】:

标签: python inheritance


【解决方案1】:

除了在方法中声明 default_headers 变量,您还应该将它们分配为对象的属性:'self.default_headers = ...'

另外请注意,强烈建议在 init 方法中声明所有参数,例如 'self.default_headers = {}'

【讨论】:

    猜你喜欢
    • 2018-02-04
    • 2019-11-27
    • 1970-01-01
    • 1970-01-01
    • 2010-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多