read, modify, use and save number

LegacyForumLegacyForum Posts: 1,669 ✭✭
edited December 2016 in SOAtest
I need to be able to read a number from a file, increment that number, use the incremented number in other data sources, and save that incremented number to the same file.

In other words, I need an incrementor that is stored between test runs.

Any ideas?
Tagged:

Comments

  • LegacyForumLegacyForum Posts: 1,669 ✭✭
    edited March 2008
    You'll be happy to know that you do not need to write to and read from a file to accomplish this task. Instead, you can do this with a script that stores the variable within the application.

    To create and access this variable you can use the following script in a Method Test:
    CODE
    from soaptest.api import *
    from com.parasoft.api import *

    SUITE_VAR = "myVar"

    #Select this in the Method dropdown to perform the required tasks
    def  doSomething(input, context):
      myVar = getMyVar(context)
      #Do what you like with the variable

      #You can return the value of your variable to use to parameterize
      #data by myVar = str(myVar) and return SOAPUtil.getXMLFromString([myVar])
      #Then, attach a Data Bank to this Method and Add the XPath of the return value
      return 1

    #This function retrieves your variable, or creates one if it doesn't exist
    def getMyVar(context):
      myVar = Application.getContext().get(SUITE_VAR)
      if myVar == None:
          myVar = "0"
          Application.getContext().put(SUITE_VAR, myVar)
      return int(myVar)

    # Call this in doSomething() to increment and save your variable
    def incrementAndSave(myVar, context):
      myVar += 1
      Application.getContext().put(SUITE_VAR, str(myVar))

    #Select this in the Method dropdown and run the Method Test to reset myVar
    def resetVar(input, context):
      Application.getContext().put(SUITE_VAR, None)
      return 1

    I have added comments in the code to describe what each method is doing.

    You can add other methods to perform various tasks in the doSomething() method. Also, you can add other Method Tests to access this variable throughout the test suite. Notice that this is reusable code because you can change the string stored in SUITE_VAR in order to have a brand new, and distinct suite variable.
Sign In or Register to comment.