<font face="arial" size="3"><p style="margin:0;padding:0;font-family: arial; font-size: 12pt; overflow-wrap: break-word;">I often do something similar, but usually with a command-line option or sub-command to indicate whether to run tests instead of doing the normal thing, and my default is usually to do the normal thing, not run tests.</p>
<p style="margin:0;padding:0;font-family: arial; font-size: 12pt; overflow-wrap: break-word;"> </p>
<p style="margin:0;padding:0;font-family: arial; font-size: 12pt; overflow-wrap: break-word;">However, many times I'll write a module not intended for use as a stand-alone script, but put some tests in it, and put an "if __name__ ==  '__main__'" thing in there that will unconditionally run tests. Then I can run that module's using the "-m" option, which I love:</p>
<p style="margin:0;padding:0;font-family: arial; font-size: 12pt; overflow-wrap: break-word;"> </p>
<p style="margin:0;padding:0;font-family: arial; font-size: 12pt; overflow-wrap: break-word;">python -m module_name</p>
<p style="margin:0;padding:0;font-family: arial; font-size: 12pt; overflow-wrap: break-word;"> </p>
<p style="margin:0;padding:0;font-family: arial; font-size: 12pt; overflow-wrap: break-word;">The test could use doctests, or even just call a function that has code with assert statements in it. I often use this technique when "bench-testing" a module under development.</p>
<p style="margin:0;padding:0;font-family: arial; font-size: 12pt; overflow-wrap: break-word;"> </p>
<p style="margin:0;padding:0;font-family: arial; font-size: 12pt; overflow-wrap: break-word;">David H</p>
<p style="margin:0;padding:0;font-family: arial; font-size: 12pt; overflow-wrap: break-word;"> </p>
<p style="margin:0;padding:0;font-family: arial; font-size: 12pt; overflow-wrap: break-word;"> </p>
<p style="margin:0;padding:0;font-family: arial; font-size: 12pt; overflow-wrap: break-word;">On Saturday, February 11, 2017 7:22pm, "kirby urner" <kirby.urner@gmail.com> said:<br /><br /></p>
<div id="SafeStyles1486864474">
<div dir="ltr">
<div>
<div>
<div>
<div>
<div>Does anyone use this pattern?:  </div>
<br />(a) accept command line parameters if __name__ == "__main__" or</div>
(b) run unittests if no parameters passed<br /><br /></div>
Something like:<br /><br />if __name__ == "__main__":<br />    if len(sys.argv)==7:<br />        command_line()  <br />    else:<br />        unittest.main()<br /><br /></div>
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. <br /><br />Maybe this would be better with doctest instead. I could do another version....<br /><br />Example:<br /><a href="https://github.com/4dsolutions/Python5/blob/master/tetravolume.py">https://github.com/4dsolutions/Python5/blob/master/tetravolume.py</a><br /><br /></div>
Kirby<br /><br /></div>
</div></font>