Forum Discussion

rprusia's avatar
15 years ago

How do I control which steps are executed in SOAP UI

I am trying to create a Test Suite that will execute the following steps.

Steps:
1. Equine 1 Horse - 70% of the time
2. Equine 2 Horses - 16% of the time
3. Equine 3 Horses - 5% of the time
4. Equine 4 Horses - 3% of the time
5. Equine 5 Horses - 3% of the time
6. Equine 10 Horses - 3% of the time

I want to execute the “Equine 1 Horse” step 70 % of the time and the others at various percentages. Below is a groovy script I created, but I’m not sure it is working.

What is the best way to do something randomly select step by percentage of execution?

Groovy Script
int intRandNum;
intRandNum = Math.random();

if (intRandNum <= 69) {
// 1 horse
testRunner.runTestStepByName("Equine 1 Horse");
}
else if ((intRandNum > 69) && (intRandNumHorses <= 85)) {
// 2 horse
testRunner.runTestStepByName("Equine 2 Horses");

}
else if ((intRandNum > 85) && (intRandNumHorses <= 91)) {
// 3 horse
testRunner.runTestStepByName("Equine 3 Horses");
}
else if ((intRandNum > 91) && (intRandNumHorses <= 94)) {
// 4 horse
testRunner.runTestStepByName("Equine 4 Horses");
}
else if ((intRandNum > 94) && (intRandNumHorses <= 97)) {
// 5 hores
testRunner.runTestStepByName("Equine 5 Horses");
}

else if ((intRandNum > 97) && (intRandNumHorses <= 100)) {
// 10 horse
testRunner.runTestStepByName("Equine 10 Horses");
}
  • You're right, it doesn't work
    I think this is because the function Math.random() returns a random number between 0 and just under 1. You've then got this as an integer, so it's always returning zero - hence the first branch (1 horse) is always being taken.

    If you make the change to the line
    intRandNum = Math.random()
    so that it's
    intRandNum = Math.random()*100
    then this will, fairly obviously, multiply the generated value by 100, and give you a number between 0 and 99.

    One other small change I think you'll need to make is replace the occassions where you have "intRandNumHorses" with "intRandNum".

    This should then, hopefully, give you the results you're looking for.

    Cheers,
    Geoff