Global Random Number for all test-cases

137 Views Asked by At

I would like to have a global variable, to be used in all of my tests, hence I configured in: citrus-context.xml the following:

<citrus:global-variables>
    <citrus:variable name="myVariableName" value="citrus:randomNumber(3)"/>
</citrus:global-variables>

This seems to work, but the generated variable is changed to a new random number, everytime I refer to this variable.

${myVariableName} ${myVariableName} ${myVariableName}
835               165               516

What is the preferred way to generate a random number to be used in all tests?

2

There are 2 best solutions below

0
On BEST ANSWER

I solved this on my own, by implementing a BeforeSuite. Thanks to this post here: How should i pass variable extracted From Payload thru test classes? So, this is how I did this:

public class InitializationTest extends TestDesignerBeforeSuiteSupport {

    @Override
    public void beforeSuite(TestDesigner designer) {
        designer.createVariable("myGlobalNumber", RandomNumberFunction.getRandomNumber(3, true));

        designer.action(new AbstractTestAction() {
            @Override public void doExecute(TestContext testContext) {
                testContext.getGlobalVariables()
                        .put("myGlobalNumber", testContext.getVariable("myGlobalNumber"));
            }
        });

    }
}
1
On

Thx for reporting this!
Unfortunately you've found a bug in Citrus. It seems like the expression is evaluated every time the variable is called. This should not be the case. I've opened this issue on GitHub to track the bug.

BR,
Sven