Locust Directory

Multiple User Logins

How to create a test that logs in as different users

Overview

It's quite common to need to login to a site, and we already have several guides on that:

This guide will give you an example of how to login as different users throughout the test.

Code

from locust import HttpUser, TaskSet, task, between
import time

class AwesomeUser(HttpUser):    
    wait_time = between(10, 20)

    # On start populate our user list
    def on_start(self): 
        self.createUsernames()

    # Create an array of user1 -> user1000
    def createUsernames(self): 
        self.users = []
        for i in range(1000):
            self.users.append("user"+str(i+1))

    # Fetch the next user in our list
    def getUsername(self):
        return self.users.pop()


    # Login to the site
    def login(self):        
        username = self.getUsername()
        loginInformation = {
            "username": username,
            "password":"passw0rd"
        }
    
        self.client.post("/login", loginInformation)

    # Logout from the site
    def logout(self):
        self.client.get('/logout', name="logout")


    # Define our tasks here
    @task(1)
    def load_page(self):
        # Login first
        self.login()

        # Put your logged in tasks here
        self.client.get('/profile/info')    
        
        # Logout now
        self.logout()

The above code creates 1000 users named "user1" through "user1000" and logs in with each of those. It's designed for a 1000 user test of course. You can easily modify this. You can also pick user names at random like so:

def getUsername(self):
    return "user"+str(randomint(1,1000))


This guide is part of the LoadForge Directory, an index of locustfile's for use with LoadForge website and API load tests. We also provide a wizard to generate tests, and onboarding assistance for clients. Contact us should you have any questions.

LoadForge provides load testing and stress tests for websites, APIs and servers. It uses your cloud account to rapidly scale large numbers of simulated users to load test your website, store, API, or application for cheap - just cents per test!

For more help on Tests please see our official documentation. Logged in users can also use our wizard to generate a locustfile, or you can record your browser steps.

Ready to run your test?
Start your first test within minutes.