1

I think the problem is with selfs/class methods etc.

first of all, I have that kind of code:

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")

The next ones @tasks/methods are waiting for default_headers,

So my question is - how to make default headers for whole claas MyUser(HttpUser)?

I think I did some mistakes with selfs, methods, etc.

So how to declare default_headers for all of the methods in this class??

Could you please help me?

Osho
  • 146
  • 8
  • [This answer](https://stackoverflow.com/a/36634227/5267751) should work, given that [locust's `HttpSession` extends `requests.Session`](https://docs.locust.io/en/stable/api.html?highlight=client#httpsession-class). – user202729 Sep 30 '20 at 01:25

1 Answers1

0

Instead of just declaring default_headers variable in the method, you should assign them as a property of the objects: 'self.default_headers = ...'

Also note that it's highly recommended to declare all parameters in the init method, like 'self.default_headers = {}'

Kobe Bryant
  • 7
  • 1
  • 7