-3

I am trying to run a test in locust, however I am getting the following exception:

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

I was writing the tests against other ones and they didn't have anything more in constructor. Do you have an idea what's wrong in this code? Let me know if you need anything more to provide.

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:

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
Beti
  • 175
  • 2
  • 10

1 Answers1

2

Inside your class Add1KECUs(HttpUser): add a function at the starting as such:

def __init__(self,HttpUser):
    pass

Hence your class will now be as:

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

    def on_start(self):

Followed by the rest of the code of the function as it is

Now where ever you're using HttpUser in code change it to self.user (except for in the __init__ function).

Now run your class as:

Add1KECUs(HttpUser)
  • when I've added such def, and now I am getting this kind of error: TypeError: __init__() takes 1 positional argument but 2 were given – Beti Sep 15 '21 at 12:08
  • Then you're calling the class the wrong way. Please tell me what code you're typing to run this class. – KingKong BigBong Sep 15 '21 at 12:17
  • I run this as - locust -f tests/add1KECUs.py in terminal – Beti Sep 15 '21 at 12:18
  • I've made a change to `__init__` function in my answer please have a look at that. Plus for the rest of the code, wherever you've used `HttpUser` change it to `self.user` – KingKong BigBong Sep 15 '21 at 12:28
  • `class Add1KECUs: def __init__(self): self.user = HttpUser` - when I used this, then i got the error: **[2021-09-15 14:47:43,439] HILDZL129399/ERROR/locust.main: No User class found!**, however when I used `class Add1KECUs(HttpUser):` then I still get the **TypeError: __init__() takes 1 positional argument but 2 were given.** :( – Beti Sep 15 '21 at 12:52
  • A new edit was made to the `__init__` function in the answer. Please recheck it. Plus Run your class as `Add1KECUs(HttpUser)` – KingKong BigBong Sep 16 '21 at 11:47