How to get return value from a .py file?

Help
Mayura
2012-07-17
2013-06-12
  • Mayura

    Mayura - 2012-07-17

    Hi,

    We are trying to run a .py file onto remote machines using STAX xml. We would like to return the execution result (pass/fail) from the python file back to the monitoring machine. Is there any way to do that using STAF/STAX.

    This is what the .py file contains -
    def testPingIP_001(self):
            """ 5.1.3.1 | Ping an IP ADDRESS """
            try :
                testcount = -1
                self.RemoteIPAddress()
                self.IP
                ob.HTML()
                for key in self.Mydict.keys():
                    testcount = testcount+1
                   
                    data = os.popen("ping " +self.Mydict).read()
                    print data

                    if 'Request timed out' in data or 'unknown host' in data:
                        print "Ping Failed Code"
                        ob.fout('<tr><td ALIGN = "center">'+str(testcount)+"</td><td>FAIL</td></tr>")
                   
                    else:
                        print "Ping Returned Something Good Code"
                        ob.fout('<tr><td ALIGN = "center">'+str(testcount)+"</td><td>PASS</td></tr>")
            except :
                print "error"

    Regards.

     
  • Arun Singh

    Arun Singh - 2012-07-17

    STAX File.. master.xml
    <!- SNIP
    <?xml version="1.0" encoding="UTF-8" standalone="no"?>
    <!DOCTYPE stax SYSTEM "stax.dtd">

    <stax>

      <defaultcall function="Main"/>

      <function name="Main">  

         <sequence>

            <message log="1">'Send a message from Python script to STAX Monitor'</message>                  

            <process name="'MyPythonScript'">
              <location>'local'</location>
              <command mode="'shell'">'python c:/STAF/trial/Client.py %s' % (STAXJobID)</command>
              <stderr mode="'stdout'"/>
              <returnstdout/>
            </process>      

            <log message="1">
              'Python Script RC=%s, STAFResult=%s, STAXResult=\n%s' % (RC, STAFResult, STAXResult)
            </log>

          </sequence>  
       
      </function>     

    </stax>
    ->

    python file  Client.py
    <!- SNIP
    from PySTAFv3 import *
    import sys

    # Get STAX Job ID passed as an argument
    staxJobID = sys.argv

    # Set to the hostname or /IP address of your STAX service machine
    # (or 'local' if the Python script is run on your STAX service machine)
    staxMachine = 'local'
    # Create a STAF handle works to use to submit STAF service requests
    try:
        handle = STAFHandle('MyPythonTest')
    except STAFException as e:
        print('Error registering with STAF, RC: %d Result: %s' % (e.rc, e.result))
        sys.exit(e.rc)

    # Send a status message to the STAX Monitor
    message = 'Python script is in Step 1'
    request = 'SEND JOB %s MESSAGE %s' % (staxJobID, wrapData(message))
    result = handle.submit(staxMachine, 'STAX', request)

    message = 'Python script is in Step 2'
    request = 'SEND JOB %s MESSAGE %s' % (staxJobID, wrapData(message))
    result = handle.submit(staxMachine, 'STAX', request)

    message = 'Python script is in Step 3'
    request = 'SEND JOB %s MESSAGE %s' % (staxJobID, wrapData(message))
    result = handle.submit(staxMachine, 'STAX', request)

    if (result.rc != STAFResult.Ok):
        print('Error: RC=%s Result=%s\nSTAF %s STAX %s' % \
           (result.rc, result.result, staxMachine, request))
        sys.exit(1)

    print('\n*** Test successful ***')

    # Un-register the STAF handle

    result = handle.unregister()

    if (result != STAFResult.Ok):
        print('Error unregistering with STAF, RC: %d' % result)

    # Test passed - Return 0
    sys.exit(0)
    ->

    It's simple .. Hope you are looking the same :-)

     
  • Sharon Lucas

    Sharon Lucas - 2012-07-17

    Your Python script, Client.py, is returning 0 when successful via sys.exit(0) and non-zero when not successful.  So, that means that process's return code will be provided in the RC variable after the process runs so you can check it.  For example:

    <sequence>

            <message log="1">'Send a message from Python script to STAX Monitor'</message>                  

            <process name="'MyPythonScript'">
              <location>'local'</location>
              <command mode="'shell'">'python c:/STAF/trial/Client.py %s' % (STAXJobID)</command>
              <stderr mode="'stdout'"/>
              <returnstdout/>
            </process>      

            <if expr="RC == 0">
              <log message="1">
                'Success: Python Script RC=%s, STAFResult=%s, STAXResult=\n%s' % (RC, STAFResult, STAXResult)
              </log>
              <else>
                <log message="1">
                  'Failed: Python Script RC=%s, STAFResult=%s, STAXResult=\n%s' % (RC, STAFResult, STAXResult)
                </log>
              </else>
            </if>    
          </sequence>  

     
  • Mayura

    Mayura - 2012-07-18

    Thank you for such a quick response! It was really helpful.

    We could talk to our python script now with the code you have provided. But going forward, we would like to excute that python script as a test case (we will add a testcase element in the xml) and success/failure of any piece of code in the .py file should result in Pass/Fail of testcase in the xml .
    Is there any way we could achieve this?

     
  • Sharon Lucas

    Sharon Lucas - 2012-07-18

    Yes.  There are a couple of ways you could do this:

    1) You can have your python script return a non-zero return code indicating a failure and then when you check the RC after the python script runs, use a <tcstatus> element to record a pass or fail.

    2) Or, you could have your Python script use STAF Python APIs provided to submit a UPDATE TESTCASE request to the STAX service.  See section "UPDATE TESTCASE" in the "Request Syntax" section of the STAX User's Guide at http://staf.sourceforge.net/current/STAX/staxug.html#Header_Update.   And, for more information on how to submit STAF service requests within a Python script, see the STAF Python User's Guide at http://staf.sourceforge.net/current/STAFPython.htm.

     
  • Mayura

    Mayura - 2012-07-18

    Thanks for your reply.
    Following is the code we tried after your suggestion:-

    def testPingIP_001(self):
            """ 5.1.3.1 | Ping an IP ADDRESS """
            message = 'Python script is in Step 1'
            request = 'SEND JOB %s MESSAGE %s' % (staxJobID, wrapData(message))
            result = handle.submit(staxMachine, 'STAX', request)

            try :
                testcount = -1
                self.RemoteIPAddress()
                self.IP
                ob.HTML()
              
                testcount = testcount+1
                data = os.popen("ping " +self.Mydict).read()
               
                if 'Request timed out' in data or 'unknown host' in data:
                   
                    message = 'Test Case Failed ' +self.Mydict
                    request = 'SEND JOB %s MESSAGE %s' % (staxJobID, wrapData(message))
                    result = handle.submit(staxMachine, 'STAX', request)
                    print "Ping Failed Code"
                    ob.fout('<tr><td ALIGN = "center">'+str(testcount)+"</td><td>FAIL</td></tr>")

                   
                    result.rc != STAFResult.Ok
                    print('Error: RC=%s Result=%s\nSTAF %s STAX %s' % \
                          (result.rc, result.result, staxMachine, request))
                                 
                else:
                   
                    #print "Ping Returned Something Good Code"
                    message = 'Test Case Passed ' +self.Mydict
                    request = 'SEND JOB %s MESSAGE %s' % (staxJobID, wrapData(message))
                    result = handle.submit(staxMachine, 'STAX', request)
                    #ob.fout('<tr><td ALIGN = "center">'+str(testcount)+"</td><td>PASS</td></tr>")
                    print "rersult rc:",result.rc
                    result.rc != STAFResult.Ok
                    print('Error: RC=%s Result=%s\nSTAF %s STAX %s' % \
                          (result.rc, result.result, staxMachine, request))
                              
                     
            except :
                   result = handle.unregister()
                   if (result != STAFResult.Ok):
                       print('Error unregistering with STAF, RC: %d' % result)
                       sys.exit(0)

    But we are only able tp get messages on our stax monitor. we are not able to fetch any testcase status whether the testcase is PASS/FAIL.Can you please suggest some additon to get a testcase status as Pass|Fail to this code.             

     
  • Sharon Lucas

    Sharon Lucas - 2012-07-18

    So, which of my suggestions did you attempt?  If you were going to attempt to submit a "UPDATE TESTCASE" request to the STAX service, the code you posted does not do this.

     
  • Sharon Lucas

    Sharon Lucas - 2012-07-18

    To update testcase status within your Python script, you should do something like the following:

    #  Assign the testcase name, or get the current testcase from your STAX job
    #  by passing the value of STAXCurrentTestcase to the Python script
    testName = 'MyTest'

    if 'Request timed out' in data or 'unknown host' in data:
        # Record a testcase fail status
        message = 'Test Case Failed ' +self.Mydict
        request = 'UPDATE JOB %s TESTCASE %s STATUS Pass MESSAGE %s' % \
                          (staxJobID, wrapData(testName), wrapData(message))
        result = handle.submit(staxMachine, 'STAX', request)
        if result.rc != STAFResult.Ok:
            print('Error: RC=%s Result=%s\nSTAF %s STAX %s' % \
                     (result.rc, result.result, staxMachine, request))
        print "Ping Failed Code"

    else:
        # Record a testcase pass status
        message = 'Test Case Passed ' +self.Mydict
        request = 'UPDATE JOB %s TESTCASE %s STATUS Pass MESSAGE %s' % \
                          (staxJobID, wrapData(testName), wrapData(message))
        result = handle.submit(staxMachine, 'STAX', request)
        if result.rc != STAFResult.Ok:
            print('Error: RC=%s Result=%s\nSTAF %s STAX %s' % \
                     (result.rc, result.result, staxMachine, request))

     
  • Sharon Lucas

    Sharon Lucas - 2012-07-18

    The description of the TESTCASE option for a STAX UPDATE request in the STAX User's Guide says:

    TESTCASE specifies the fully qualified name of the testcase whose status is to be updated. The testcase specified must already exist, unless FORCE is also specified. If you wanted a process defined by a <process> element in a job to update the status of the current testcase, the value of the STAXCurrentTestcase Python variable could be passed to the process via the <parms> element or <env> element and an UPDATE request could be submitted from within the process, specifying the current testcase value passed in for the TESTCASE option.

    So, you may want to use a <testcase name="'Your testcase name'"> element within your STAX job to create a testcase and then pass the value of STAXCurrentTestcase to your Python script (like you are already passing in the STAXJobID).  Otherwise, you'll need to use the FORCE option on the UPDATE request you are submitting in your Python script to force the creation of a testcase.

     
  • Mayura

    Mayura - 2012-07-20

    Thanks for your suggestions.we are now facing problem while importing unittest module in python for running group of test cases.
    Following error is coming on our stax monitor:

    Failed: Python Script RC=1, STAFResult=None, STAXResult= [\r\n File "C:\\Python27\\lib\\unittest\\loader.py", line 100, in loadTestsFromName\r\n parent, obj = obj, getattr(obj, part)\r\nAttributeError: \'module\' object has no attribute \'219\'\r\n']]

    we have also tried solving this problem  by importing unittest externally from C:\\Python27 but are not able in succeeding that.

    warm Regards,
    staftest

     
  • Sharon Lucas

    Sharon Lucas - 2012-07-20

    This appears to be a Python question as it is not related to STAF/STAX specifically.  I don't know anything about Python's unittest module.  Did you try running your Python script manually (e.g. from a command line)?  If it fails as well, then you should try asking this question on a Python forum or google for this error.

     
  • Mayura

    Mayura - 2012-07-23

    I am able to run unit tests in python ,but i am facing problem in executing multiple testcases present in a same .py file or you can say executing a whole testsuite.presently my xml file is as follows:
    <?xml version="1.0" encoding="UTF-8" standalone="no"?>
    <!DOCTYPE stax SYSTEM "stax.dtd">

    <stax>

      <defaultcall function="Main"/>

      <function name="Main">  

    <testcase name="'MyTest'">

    <sequence>

    <message log="1">'Send a message from Python script to STAX Monitor'</message>                  

    <process name="'MyPythonScript'">
      <location>'local'</location>
      <command mode="'shell'">'python D:/Project/Script.py %s' % (STAXJobID)</command>
      <stderr mode="'stdout'"/>
      <returnstdout/>
    </process>      

    <if expr="RC == 0">
      <log message="1">
    'Success: Python Script RC=%s, STAFResult=%s, STAXResult=\n%s' % (RC, STAFResult, STAXResult)
      </log>
      <else>
    <log message="1">
      'Failed: Python Script RC=%s, STAFResult=%s, STAXResult=\n%s' % (RC, STAFResult, STAXResult)
    </log>
      </else>
    </if>    
      </sequence>  
     
        </testcase>

      </function>     

    </stax>
    and my test cases in python file is as follows:-
    import Project
    import os
    import sys
    import datetime
    import unittest

    class STAFTestcase(unittest.TestCase):

        def setUp(self):
            self.a = Project.RPT()
            self.a.handle()
            self.a.RemoteIPAddress()
            self.a.CurrentMachineIP()
            pass

       
        def testPingIP_001(self):
       
          
            """ 5.1.3.1 | Ping an IP ADDRESS """
            self.a.message('Python script is in TestCase 1')
            testName ="MyTest"
            testcount=1
            try :
                self.a.HTML()
                self.a.RemoteIPAddress()
                self.a.IP
                data = os.popen("ping " +self.a.Mydict).read()
               
                if 'Request timed out' in data or 'unknown host' in data:
                   
                    # Record a testcase fail status
                    self.a.failcase('Test Case Failed ' +self.a.Mydict,testName)
                    self.a.fout('<tr><td ALIGN = "center">'+str(testcount)+"</td><td>FAIL</td></tr>")
                    self.a.checkstafresult()
                   
                else:
                    # Record a testcase pass status
                    self.a.passcase('Test Case Passed ' +self.a.Mydict,testName)
                    self.a.fout('<tr><td ALIGN = "center">'+str(testcount)+"</td><td>PASS</td></tr>")
                    self.a.checkstafresult()
                   
                                  
            except :
                self.a.handleunregister()

        def testPingIP_002(self):
            """ 5.1.3.1 | Ping all IP ADDRESS"""
            self.a.message('Python script is in TestCase 2')
            testName ="MyTest1"
            testcount=2
            try :
                print a.Mydict.keys()
                for key in self.a.Mydict.keys():
                   
                    data = os.popen("ping " +self.a.Mydict).read()
                    if 'Request timed out' in data or 'unknown host' in data:
                        self.a.failcase('Test Case Failed at ' +self.a.Mydict,testName)
                        self.a.fout('<tr><td ALIGN = "center">'+str(testcount)+"</td><td>FAIL</td></tr>")
                        self.a.checkstafresult()
                        break
                    else:
                        self.a.passcase('Test Case Passed',testName)
                        self.a.fout('<tr><td ALIGN = "center">'+str(testcount)+"</td><td>PASS</td></tr>")
                        self.a.checkstafresult()
                      
            except:
                self.a.handleunregister()

        def tearDown(self):
            pass
                       

               
    if __name__ == '__main__':
       
        unittest.main(argv=[sys.argv])

    Kindly help me or suggest any changes in xml file so that i am able to execute both test cases one by one and will be able to see the result of both test cases on Stax monitor.
    currently i am able to execute only single test case and see the result of that testcase only.

    Warm Regards
    staftest

     
  • Sharon Lucas

    Sharon Lucas - 2012-07-23

    I don't think you posted the code that shows me that running your Python script actually calls both functions testPingIP_001 and testPingIP_002.  And you didn't post the code that actually submits an UPDATE request to the STAX service to update the testcase status for these testcases.

     
  • Mayura

    Mayura - 2012-07-24

    I basically want to know the format of a xml file for calling  two test cases and displaying their result in sequence.In the above example of mine I am able to see the result of only one test case testPingIP_001 in a Stax monitor.so I want to know the changes made in XML file so that I am able to see the result  of testPingIP_002 also on Staxmonitor.Please don't worry about python code only let us know the format of XML file to view result for say multiple test cases present ina same .py

    Regards,
    staftest

     
  • Sharon Lucas

    Sharon Lucas - 2012-07-24

    There isn't a particular "format" for a STAX xml file in order to have results for multiple testcases.  Basically, you just have to use a different testcase names when recording a testcase status.  Note you can have multiple passes/fails for each testcase if desired.  For example, the following job would show the following testcase results:

        MyTest - 1 pass
        MyTest1 - 2 passes

    <function name="Main">  
      <sequence>

        <testcase name="'MyTest'">
          <sequence>
            …
            <tcstatus result="'pass'"/>
          </sequence>
        </testcase>

        <testcase name="'MyTest1'">
          <sequence>
            …
           <tcstatus result="'pass'"/>
            …
           <tcstatus result="'pass'"/>
          </sequence>
        </testcase>

      </sequence>
    </function>

    The testcase element is a wrapper which defines a testcase and contains a single task element for which testcase status is recorded. The testcase element is used in conjunction with the tcstatus element to increment testcase pass/fail counters.  You may nest testcase elements. For nested testcases, the testcase name will be recorded in the form of ParentTestcase.ChildTestcase in the STAX logs and queries (so don't use periods, ".", in your testcase names).

    For example, the following STAX code shows using nested testcases and would show the following testcase results:

       Scenario01.TestA - 1 pass
       Scenario01.TestB - 1 pass

    <function name="Main">  
      <sequence>

        <testcase name="'Scenario01'">
          <sequence>

            <testcase name="'TestA'">
              <sequence>
                …
                <tcstatus result="'pass'"/>
              </sequence>
            </testcase>

            <testcase name="'TestB'">
              <sequence>
                …
                <tcstatus result="'pass'"/>
              </sequence>
            </testcase>

          </sequence>
        </testcase>
       
      </sequence>
    </function>

    These examples showed starting testcases using the <testcase> element and updating status for a <testcase> element using the <tcstatus> element.  Another way to update testcase status is using the STAX UPDATE TESTCASE request (e.g. used by a command run via a <process> element like you are doing by running a Python script that submits this request).  Using the STAX UPDATE TESTCASE request, you can record a testcase status for an existing testcase (e.g. one that you have started via a <testcase> element that your STAX job).  Or you can record testcase status for a new testcase by either  using the FORCE option on the STAX UPDATE TESTCASE request or, your Python script can submit a STAX START TESTCASE request which basically performs the same action as a <testcase> element with the 'strict' mode.  See the STAX User's Guide at http://staf.sourceforge.net/current/STAX/staxug.html#Header_Start for more information on the STAX START TESTCASE request.

    So, for example, a function in your STAX xml job could use a <process> element to run a Python script as follows.

    <function name="Main">
      <sequence>

        <process name="'MyPythonScript'">
          <location>'local'</location>
          <command mode="'shell'">'python D:/Project/Script.py %s' % (STAXJobID)</command>
          <stderr mode="'stdout'"/>
          <returnstdout/>
        </process>
        …
      </sequence>
    </function>

    If your Python script submitted for following STAX UPDATE TESTCASE requests using the FORCE option:

      STAF local STAX UPDATE TESTCASE "MyTest" JOB <STAXJobID> STATUS Pass FORCE MESSAGE "This test passed"
      STAF local STAX UPDATE TESTCASE "MyTest1" JOB <STAXJobID> STATUS Pass FORCE MESSAGE "This test passed"

    then the STAX job's testcase results would look like:

        MyTest - 1 pass
        MyTest1 - 1 pass

    Or, you could use nested testcases if desired.  Perhaps you wanted to see the following testcase results:

      PingTest.machineA - 1 pass
      PingTest.machineB - 1 pass

    Then, one way you can do this is to have a STAX job that contains <testcase name="'PingTest'"> and then have this testcase start a process via the <process> element that submits the following STAX UPDATE TESTCASE requests:

      STAF local STAX UPDATE TESTCASE "PingTest.machineA" JOB <STAXJobID> STATUS Pass FORCE
      STAF local STAX UPDATE TESTCASE "PingTest.machineB" JOB <STAXJobID> STATUS Pass FORCE

    I hope this helps.  See the STAX User's Guide for more examples.

     
  • Mayura

    Mayura - 2012-07-25

    Thank You! It did help us :)

     

Log in to post a comment.

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks