[Chicago] __name__ == "__main__"

Ian Bicking ianb at colorstudy.com
Fri Oct 26 18:33:05 CEST 2007


Kumar McMillan wrote:
> On 10/26/07, Michael Tobis <mtobis at gmail.com> wrote:
>> Hi, Ian! Thanks for trying.
>>
>> Unfortunately, I still don't actually see how main() is better than
>> just putting what you have in main() inline under the __name__
>> conditional...
> 
> my opinion is that it's easier to test, that's all.  If you are trying
> to functionally test your main code, I don't think there is a way
> without creating a main function.  For example :
> 
> from mymodule import main
> def test_mymodule_command():
>     try:
>         main(['--some-option=foo'])
>     except SystemExit, e:
>         assert e.code == 0
>     # now you can do the post-mortem

Or you can use ScriptTest: http://pythonpaste.org/scripttest/ :

from scripttest import TestFileEnvironment
def test_mymodule_command():
     env = TestFileEnvironment('./test-output')
     res = env.run('myscript', '--some-option=foo')
     assert 'sucess' in res.stdout


-- 
Ian Bicking : ianb at colorstudy.com : http://blog.ianbicking.org


More information about the Chicago mailing list