I’m writing Locust scripts and want to extend a base class. There are two values (dto_class
and api_suffix
) that are different from one sub-class to another. Everything else is shared
locust_base.py
(base class):
### locust_base.py ###
from locust import HttpUser, between, task
## other imports ##
class BaseUser(HttpUser):
"""
do some stuff
"""
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.dto_class = kwargs.pop('dto_class')
self.api_suffix: str = kwargs.pop('api_suffix')
"""
set other stuff
"""
The sub-classes (e.g. locust_specific_dto.py
) look like:
"""
locust_specific_dto.py
"""
from locust_base import BaseUser
class SpecificDtoUser(BaseUser):
def __init__(self, *args, **kwargs):
super(SpecificDtoUser, self).__init__(dto_class=SpecificDto, api_suffix="specificEndpoint", *args, **kwargs)
However, when running locust -f locust_specific_dto.py
, this error gets output:
KeyError: 'dto_class'
2024-05-09T18:09:14Z <Greenlet at 0x21e4d99d580: <lambda>> failed with KeyError
[2024-05-09 14:09:14,811] WSAMZN-SPLR8QE1/CRITICAL/locust.runners: Unhandled exception in greenlet: <Greenlet at 0x21e4d99d580: <lambda>>
Traceback (most recent call last):
File "src\gevent\greenlet.py", line 908, in gevent._gevent_cgreenlet.Greenlet.run
File "{REDACTED}AppDataRoamingPythonPython311site-packageslocustrunners.py", line 553, in <lambda>
lambda: self._start(user_count, spawn_rate, wait=wait, user_classes=user_classes)
^^^^^^^^^^^^^^^^^
File "{REDACTED}AppDataRoamingPythonPython311site-packageslocustrunners.py", line 524, in _start
self.spawn_users(user_classes_spawn_count, wait)
^^^^^^^^^^^^^^^^^
File "{REDACTED}AppDataRoamingPythonPython311site-packageslocustrunners.py", line 238, in spawn_users
new_users += spawn(user_class, spawn_count)
^^^^^^^^^^^^^^^^^
File "{REDACTED}AppDataRoamingPythonPython311site-packageslocustrunners.py", line 227, in spawn
new_user = self.user_classes_by_name[user_class](self.environment)
^^^^^^^^^^^^^^^^^
File "{REDACTED}Documentsdev{REDACTED}locust_base.py", line 23, in __init__
self.dto_class = kwargs.pop('dto_class')
^^^^^^^^^^^^^^^^^
KeyError: 'dto_class'
New contributor
FAESonata is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.