Print Failure or success based on the value of the standalone tool

Ganesh Pal ganesh1pal at gmail.com
Thu May 10 12:48:39 EDT 2018


I have to test a standalone tool from a python script and I am using
os.system() to run the tool . I need to take decision based on the return
value of the standalone tool .

But since os.system merely throws the output value to STDOUT  & returns the
exit status (0 => no error) of the shell , how can I make this print Fail
or Pass based on the return value of the standalone tool.

In the below example ( return_values() function)



Example:



# cat standalone_tool.py

#!/usr/bin/env python

# script name: standalone_tool.py

import random

def return_values():

    """ My Demo function"""

    # After Execution the actual tools returns True, False, None as return
types

    x = random.choice([True, False, None])

    print x

    return x

return_values()



>>> ret = os.system('python standalone_tool.py')

True

>>> ret

0



 # vi test_standalone_tool.py

#!/usr/bin/env python

import os

# script name: test_standalone_tool.py



for i in range(2):

    retcode = os.system('python standalone_tool.py')

    print retcode

    if retcode:

        print "The standalone tool returned False. Failure"

    else:

        print "The standalone tool returned True. Successful"



1# python test_standalone_tool.py

None

0

The standalone tool returned True. Successful

True

0

The standalone tool returned True. Successful



The above problem is because we are referring to the exit status of the
shell, Is there an easy way to print Failure or success based on the value
returned by python standalone_tool.py .  I am on Linux with Python 2.7 and
sorry for the longish post


Regards,

Ganesh



More information about the Python-list mailing list