Service Virtualization

 View Only
  • 1.  How can I use Python script in DevTest script step?

    Posted Nov 16, 2015 10:14 AM

    A simple javascript like this

    function square(x){
    return x*x;
    }

    square(5);

     

    when executed displays

    Message "Script executed. Result is 25.0"

     

    But a similar python script executes but returns no result. ( fyi, I selected python as scripting language in dropdown option)

    def square(x):
        return x*x

    if __name__ == "__main__":
        square(5)

     

    Message "Script executed without a result"

     

    what am I doing wrong? and is there a better way to use Python in DevTest?



  • 2.  Re: How can I use Python script in DevTest script step?
    Best Answer

    Broadcom Employee
    Posted Nov 16, 2015 10:44 AM

    According to the DevTest 8.0 Scripting Guide ...

    "A script in ‘Execute Script (JSR-223)’ test steps does have to supply a specific return value. If no return value is specified the last evaluated expression is taken as the script’s response. This applies to BeanShell, Groovy, and JavaScript."

     

    From the behaviour you're experiencing, it looks like Python doesn't return evaluated expressions. This might include "return", I suppose?

    Python scripting isn't supplied with DevTest, so I presume someone at your site found a JSR-223 Python / Jython JAR plug-in and copied it to the DevTest installation.

     

    You could specifically set a property value from within your script:

    testExec.setStateValue("mySquareProperty", square(5));

     

    Rick



  • 3.  Re: How can I use Python script in DevTest script step?

    Posted Nov 16, 2015 11:59 AM

    setStateValue worked. having an explicit return statement throws error. Thanks!