Forum Discussion

zenpicker's avatar
zenpicker
Occasional Contributor
14 years ago

Globally visible data generators

Is there a way to build a data generator and then reference it in multiple test cases? I see the Shared property, but my understanding is that this shares it between threads in the same test case. What I am hoping for is the ability to define a generator once (e.g., one that uses a list of standardized values, like US state codes, that are referenced all over the place) and simply reference it as needed. Can I do this?

3 Replies

  • M_McDonald's avatar
    M_McDonald
    Super Contributor
    I am not sure what a data generator for states would do - return a state randomly? If you wanted that, you could put something like this in the Project Load script:

    project.metaClass.static.getState = {
    def states = ['AL','AZ','CA']
    def rand = new java.util.Random()
    return states[rand.nextInt(3)]
    }


    and wherever you wanted your random state in a Groovy script you could use

    def state = context.testCase.testSuite.project.getState()


    of as a property expansion

    ${=context.testCase.testSuite.project.getState()}
  • zenpicker's avatar
    zenpicker
    Occasional Contributor
    Very helpful - thanks! Still would love to do it without scripting, but if I can't, I can't.