Search code examples
locust

Problem updating Locust script to 1.x: TypeError: __init__() takes 1 positional argument but 2 were given


Having changed (0.x style)

class MyBaseLocust(Locust):
    def __init__(self):
        super(MyLocust, self).__init__()

to (1.x style)

class MyBaseUser(User):
    def __init__(self):
        super(MyBaseUser, self).__init__()

I get:

[2020-07-17 14:16:33,694] XXX/CRITICAL/locust.runners: Unhandled exception in greenlet: <Greenlet at 0x28639396378: <lambda>>
Traceback (most recent call last):
...
 in spawn_users
    hatch()
  File "c:\venv\project\lib\site-packages\locust\runners.py", line 165, in hatch
    new_user = user_class(self.environment)
TypeError: __init__() takes 1 positional argument but 2 were given

(this has been asked a couple of times so I thought I'd add it here)


Solution

  • Here’s how it should be in 1.x

    class MyBaseUser(HttpUser):
        abstract = True
        def __init__(self, parent):
            super().__init__(parent)
    

    (the main thing is the added parent parameter, but abstract is needed to avoid registering the base user as something that should be executed)