Python:\uuuu init\uuuu()缺少1个必需的位置参数:

2024-09-29 19:32:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试在Hocust中运行测试,但遇到以下异常:

Python: __init__() missing 1 required positional argument: 'ecu_model_id'.

我正在编写针对其他测试的测试,他们在构造函数中没有更多内容。你知道这段代码有什么问题吗? 如果你还需要什么,请告诉我

from locust import task, HttpUser
from apicall.ecu import EcuModelApi
from common.configuration import config

ECU_TO_CREATE_1K = 1000

ECU_NAME = config['vehicle']['ecuModel']

global_counter = 0
lock = threading.Lock()


class Add1KECUs(HttpUser):

    def on_start(self):
        super().on_start()
        self.ecu_model_id = EcuModelApi(self.client).get_ecu_model_id_by_name(ECU_NAME)
        self.ecu_api = EcuModelApi(self.client, self.ecu_model_id)

回溯:

Traceback (most recent call last):
  File "src/gevent/greenlet.py", line 906, in gevent._gevent_cgreenlet.Greenlet.run
  File "/home/bgryczka/pythonEnvironment/lib/python3.8/site-packages/locust/user/users.py", line 166, in run_user
    user.run()
  File "/home/bgryczka/pythonEnvironment/lib/python3.8/site-packages/locust/user/users.py", line 132, in run
    self.on_start()
  File "/home/bgryczka/dev/sources/locust-swm-performance-tests/tests/add1KECUs.py", line 19, in on_start
    self.ecu_model_id = EcuModelApi(self.client).get_ecu_model_id_by_name(ECU_NAME)
TypeError: __init__() missing 1 required positional argument: 'ecu_model_id'
2021-09-15T11:09:55Z <Greenlet at 0x7f4351a65e10: run_user(<add1KECUs.Add1KECUs object at 0x7f4351193070>)> failed with TypeError

Tags: runinpyselfidmodelonline
1条回答
网友
1楼 · 发布于 2024-09-29 19:32:27

class Add1KECUs(HttpUser):中,在开头添加一个函数,如下所示:

def __init__(self,HttpUser):
    pass

因此,您的class现在将为:

class Add1KECUs():
    def __init__(self,HttpUser):
        self.user = HttpUser;

    def on_start(self):

然后是函数的其余代码

现在,在代码中使用HttpUser的地方,将其更改为self.user(在__init__函数中除外)

现在以以下方式运行您的类:

Add1KECUs(HttpUser)

相关问题 更多 >

    热门问题