Testing functions via command line

P

pelon

There must be a couple of lines that will replace the following:
From the shell command line I wanted to send data to a specific
function inside my module and execute that function in order to test
it separately from the rest of the module. I know pdb will allow me to
insert data into a running process, but this particular function
requires several long steps beforehand.

My command line:

myprog.py --test removefromcue "myargs" ### removefromcue is
my function inside myprog.py


main()
....
try:
opts, args = getopt.getopt(sys.argv[1:], "t:", ["--test="])
except getopt.GetoptError:
for o, a in opts:
if o in "-t":
myfunc = a
myargs = args
test(myfunc, myargs)
sys.exit()


My problem was that I send a string into the the test function. I
needed to convert that string into the appropriate object. globals()
provides a dictionary with string:eek:bject pairs in the global
namespace.

def test(myfunc, myargs):
""" execute a given function """
for key,value in globals().items():
if myfunc in key:
func = value
break
args = ""
for i in myargs:
args += i + ","
func(args[:-1]) ### removefromcue("myitem")

There's something very basic that I should know.
 
S

Steven D'Aprano

There must be a couple of lines that will replace the following:

Yes, because otherwise the terrorists will have won. *wink*


From the shell command line I wanted to send data to a specific
function inside my module and execute that function in order to test
it separately from the rest of the module.

For manual testing, why don't you do your testing from the interactive
Python shell?

e.g.

$ python
Python 2.3.4 (#1, Oct 11 2006, 06:18:43)
[GCC 3.4.6 20060404 (Red Hat 3.4.6-3)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
Am I missing something?

Also, while your self-test code is very ingenious, perhaps you would be
better off using standard testing modules. Perhaps you should use the
doctest and/or unittest modules.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top