[Edu-sig] dual use __main__: work or test

kirby urner kirby.urner at gmail.com
Sat Feb 11 19:22:38 EST 2017


Does anyone use this pattern?:

(a) accept command line parameters if __name__ == "__main__" or
(b) run unittests if no parameters passed

Something like:

if __name__ == "__main__":
    if len(sys.argv)==7:
        command_line()
    else:
        unittest.main()

I'm using this pattern with my classes, as a way of touching on both
passing arguments from the command line (with nod to argparse for POSIX
compliance), and self-contained testing.

Maybe this would be better with doctest instead. I could do another
version....

Example:
https://github.com/4dsolutions/Python5/blob/master/tetravolume.py

Kirby
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/edu-sig/attachments/20170211/b4878dd9/attachment.html>


More information about the Edu-sig mailing list