Writing Performance Tests in Grinder using a Framework

by vivin

A Login and Logout Scenario

Our first scenario is going to be a very simple one. We’re just going to log into the application and then log out. We’re going to use default values and aren’t going to parameterize anything:

login-logout.py

from torqueo.test.framework.Scenario import Scenario
from torqueo.test.tasks.login.LoginTask import LoginTask
from torqueo.test.tasks.login.LogoutTask import LogoutTask

#
# Make a new scenario
#

myScenario = Scenario("My Scenario", {"url0":"https://local.sitetotest.com:8443"});

# Create a new instance of the Login Task
loginTask = LoginTask()

# Create a new instance of the Logout Task
logoutTask = LogoutTask()

#Add the tasks to our scenario
myScenario.addTask(loginTask)
myScenario.addTask(logoutTask)


class TestRunner:
   def __call__(self):
      myScenario.run()

Pretty simple, right? First we create an instance of a Scenario. We give it a description and also a dictionary containing the URL we’re going to be using. Then, we create an instance of a login task, a logout task, and add it to our scenario. Finally, we run our scenario in the __call__ method. Now what if we want to parameterize the logins? That’s not difficult either:

login-logout-parameterized.py:

from torqueo.test.framework.Scenario import Scenario
from torqueo.test.tasks.login.LoginTask import LoginTask
from torqueo.test.tasks.login.LogoutTask import LogoutTask

#
# Make a new scenario
#

myScenario = Scenario("My Scenario", {"url0":"https://local.sitetotest.com:8443"});

# Create a new instance of the Login Task
loginTask = LoginTask()

# Define our login credentials

loginCredentials = [{"username":"vivin", "password":"[email protected]"},
                    {"username":"jimbo", "password":"[email protected]"},
                    {"username":"hippy", "password":"[email protected]"},
                    {"username":"flippy", "password":"[email protected]"},
                    {"username":"batman", "password":"[email protected]"},
                    {"username":"beverly", "password":"[email protected]"},
                    {"username":"worf", "password":"[email protected]"},
                    {"username":"deanna", "password":"[email protected]"},
                    {"username":"jean-luc", "password":"[email protected]"},
                    {"username":"laren", "password":"[email protected]"},
                    {"username":"will", "password":"[email protected]"},
                    {"username":"tasha", "password":"[email protected]"},
                    {"username":"geordi", "password":"[email protected]"},
                    {"username":"data", "password":"[email protected]"},
                    {"username":"wesley", "password":"[email protected]"}]

# Set loginCredentials as a new property of loginTask since we're going to be
# using it in our parameterizing method
setattr(loginTask, "loginCredentials", loginCredentials)

# Define our parameterizing method

def parameterizeLogin(self=loginTask):
    loginCredential = self.loginCredentials[grinder.threadNumber];
    username = loginCredential["username"];
    password = loginCredential["password"];

    self.parameters["appLogin2"]["201"]["username"] = username;
    self.parameters["appLogin2"]["201"]["password"] = password;

# Set parameterizeLogin the parameterizing method for "appLogin2"
loginTask.setParameterizingMethodFor("appLogin2", parameterizeLogin)

# Create a new instance of the Logout Task
logoutTask = LogoutTask()

#Add the tasks to our scenario
myScenario.addTask(loginTask)
myScenario.addTask(logoutTask)

class TestRunner:
   def __call__(self):
      myScenario.run()