Testing python command line apps -- Running from within the projects w/o installing

Göktuğ Kayaalp self at gkayaalp.com
Fri Nov 1 05:10:43 CET 2013

On Fri, Nov 01, 2013 at 10:42:23AM +1100, Ben Finney wrote:
> Göktuğ Kayaalp <self at gkayaalp.com> writes:
> > I'm using python to write command line apps from time to time. I
> > wonder *what is the conventional way to test-run these apps from
> > within the project itself, while developing, without installing*.
> [So] the advice for testing the program's main module specialises to: Keep
> the body of “if __name__ == '__main__':” to an absolute minimum. Put all
> of the set-up and process-end functionality into discrete functions with
> discrete purposes, clear return values, and explicit parameters.

This is usually the way I write my modules, and I do test my command
line interfaces in a manner similar to your example.  But what I was
asking is how to actually run these programs within a command shell
session and test them in a more *tangible* fashion:

    (app) ~/app% ls
    scripts/ app/ tests/ setup.py LICENSE README
    (app) ~/app% cat scripts/app
    #!/usr/bin/env python
    from app import main; import sys
    if __name__ == '__main__': exit(main.main(sys.argv[1:]))
    (app) ~/app% scripts/app -CMdargs -- testdata.txt
    Traceback (most recent call last):
      File "app", line 1, in <module>
        from app import main; import sys
    ImportError: no module named 'app'

I write a secondary script, 'app_tester', which is similar to 'app', but
before running app.main.main, it inserts ~/app to the front of sys.path.
What I want to find out is how to achieve this w/o having a secondary
script that manipulates the path.

> This is adapted from an article by Guido van Rossum
> <URL:http://www.artima.com/weblogs/viewpost.jsp?thread=4829>.

I'll definitely give this a read.

> [...]

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 836 bytes
Desc: Digital signature
URL: <http://mail.python.org/pipermail/python-list/attachments/20131101/9c0465a0/attachment.sig>

More information about the Python-list mailing list