Description

The SwitchGen Generator emulates a Java switch statement. You can define multiple cases (conditions), and the generator will generate the value of the matched case. This generator is useful for simulating decision-making or conditional logic during data generation. 


For example, when testing an application with different user roles (e.g., Admin, User, Guest), the SwitchGen Generator can determine what action to take based on the 'role' variable. 


In This Article

Video Training


Generator Parameters

The following parameters may be configured for the SwitchGen Generator. Items with an asterisk* are required. 

  • switch* - Defines the referenced variable that will be used to determine the switch.
  • default* - Defines the value that is selected if no switches are met.
  • caseList - Defines a list of switches. 
    • We recommend using no more than 15 values. 
    • If more than 15 are needed, it is best practice to use the MapCSVGen Generator to obtain a mapped value. 
  • actionList - Defines the list of actions to be taken when a given switch is matched. Actions may be references to another Attribute's generated value. 



Additional Information

We recommend that you look at the SwitchGen Linked Generator Design Pattern to make the most of this generator.  


Example 1 - Generate a Security Level Based on Employee Position

A tester wants to generate a user's security level based on the employee's position: 

  • Manager = 5
  • Supervisor = 3
  • Entry-Level = 1
  • Terminated = 0


Two Attributes will be used to accomplish this: 

  • employeePosition - generates a position for each employee (manager, supervisor, entry-level, terminated).
  • securityLevel - generates the security level based on the employee's position (0, 1, 3, 5).


The desired output is shown below: 


employeeLevel Attribute

This Attribute uses a ListGen to generate one of four employee levels: Manager, Supervisor, Entry Level and Terminated.



securitylevel Attribute

This Attribute uses a SwitchGen to generate a security level for each employee. The following parameters have been configured:

  • switch - references the employeeLevel Attribute to obtain the value. 
  • default - generates a '0' for any value that is not defined in the caseList. 
  • caseList - contains cases that are checked against the referenced employeeLevel Attribute. 
  • actionList - contains the corresponding action for each case. 


The security level will be generated based on which case is 'true.' When the value does not match the defined cases (i.e., Terminated), the security level will be '0".



Example 2 - Calculate New Account Balance Based on Transaction Type

A tester wants to calculate a new account balance based on the transaction type: :

  • Withdrawal = balance - transactionAmount
  • Deposit = balance + transactionAmount
  • Transfer = balance - transactionAmount


The generated output would appear similar to what is shown below: 


This example uses four Attributes: balance, transactionType, transactionAmount, and newBalance


balance Attribute

This Attribute uses a RandomMoneyGen to generate a balance between $50.00 and $10,000.00.



transactionType Attribute

This Attribute uses a ListGen to generate the three types of transactions: Withdrawal, Deposit, and Transfer



transactionAmount Attribute

This Attribute uses a RandomMoneyGen to generate a transaction amount between $10.00 and $1000.00. 



newBalance Attribute

This Attribute uses CalcGen generators to add or subtract the transaction amount from the balance and a SwitchGen to determine which calculation to use based on the transaction type. 

  • Withdrawal - references gen1, which subtracts the transaction amount from the balance.
  • Deposit - references gen 2, which adds the transaction amount to the balance.
  • Transfer - the calculation is the same as 'Withdrawal,' and it references gen 1 as well. 





CalcGen (gen1) - Withdrawal or Transfer Calculation


CalcGen (gen 2) - Deposit Calculation