Am 18.05.2010 22:49, schrieb Serdar Tumgoren:
Hello all,

Does anyone have advice for writing unit tests against variables set by command-line options?

I have a program I'd like to run in either "debug" or "live" mode, with various settings associated with each. Below is some pseudo-code that shows what I'd like to do:

<<snipped argparse import and options setup >>
mode = p.parse_args() #always set to either --debug or --live

if mode.live:
recipients = ['jsm...@email.com <mailto:jsm...@email.com>', 'jane...@email.com <mailto:jane...@email.com>']
    # set logging to a file
elif mode.debug:
    recipients = ['ad...@admin.com <mailto:ad...@admin.com>']
    # log to stdout

The "live" and "debug" attributes are set by command-line flags passed to the argparse module. What I'd like to do is write tests that check whether various settings (recipients, logging, etc.) are configured properly based on the command-line options.

But if "mode" is not set until runtime, I clearly can't import it into my suite of unit tests, right? Is there some standard testing approach to this problem (perhaps mocking?) that you all can recommend?

I'd greatly appreciate it.
Serdar


_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
I just had a look at the optparse documentation ... huuu, quite heavier than I have expected.... But to your question:

You could reorganise your main module. Put all your code which is on module level into a function called "main" with mode as argurment and add the neat "if __name__ == "__main__" condition at the end of your module to parse the command line options and call your main function. When you import your module to your test, you have to call the "main" function "manually" and can pass a mock for the mode as required. Let's say your main module is called "serdars_main_module"

serdars_main_module.py:
--------------------------
########################################################
def main(mode):
    # all the program logic
     if mode.live:
recipients = ['jsm...@email.com <mailto:jsm...@email.com>', 'jane...@email.com <mailto:jane...@email.com>']
        # set logging to a file
     elif mode.debug:
        recipients = ['ad...@admin.com <mailto:ad...@admin.com>']
        # log to stdout
    # ...

if __name__ == "__main__":
    mode = p.parse_args() #always set to either --debug or --liv
    main(mode)
#########################################################

Then your test module could look like:

serdars_test_module.py:
-------------------------
#########################################################
import serdars_main_module
import unittest

class ArgParseMock(object):
    def __init__(self, live, debug):
        self.live = live
        self.debug = debug

class TestDebugMode(unittest.TestCase):
    def test_live_mode(self):
mode = ArgParseMock(True, False) # create the mock for the arguments
        serdars_main_module.main(mode) # call the main logic with the mock
        # ....
    def test_debug_mode(self):
mode = ArgParseMock(False, True) # create the mock for the arguments
        serdars_main_module.main(mode) # call the main logic with the mock
        # ....
##########################################################

Hope that helps,

Jan
_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Reply via email to