Testing functions via command line

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • pelon

    #1

    Testing functions via command line

    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(s ys.argv[1:], "t:", ["--test="])
    except getopt.GetoptEr ror:
    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:object 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.

  • Steven D'Aprano

    #2
    Re: Testing functions via command line

    On Mon, 02 Apr 2007 22:32:26 -0700, pelon wrote:
    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.
    >>>
    >>import my_module
    >>my_module.fun ction('somethin g') # test the function
    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.



    --
    Steven D'Aprano

    Comment

    Working...