Re: [Tutor] Unit testing command-line options from argparse or optparse

2010-05-19 Thread Serdar Tumgoren
Ah, okay -- both of those options make sense. I'll try my hand at Jan's
first and will report back if I have any problems.

Many thanks as always!
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Unit testing command-line options from argparse or optparse

2010-05-19 Thread Dave Angel

Serdar Tumgoren wrote:

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:

<>
mode = p.parse_args() #always set to either --debug or --live

if mode.live:
recipients = ['jsm...@email.com', 'jane...@email.com']
# set logging to a file
elif mode.debug:
recipients = ['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

  
I don't see the problem.  If 'mode' is a global in module  doit.py, then 
just use

   import doit
and later,
   if doit.mode.debug

The only tricky thing is to make sure that the initialization code has 
been run before you actually use the latter code.


And, presuming mode is an object of a special class made for the 
purpose, you could go a bit further.  You could create the object (with 
default attributes) in the top-level code of doit.py.  That way it 
already exists when the import is finished.  And you could then use

   from doit import mode

Now, change the semantics of parse_args() so that it takes the mode 
object as a parameter, and modifies it according to the arguments.  As 
long as you don't reassign mode itself, the test code could continue to 
use its own "variable".  But once again, don't use the mode.debug until 
the initialization has been done.


DaveA

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


Re: [Tutor] Unit testing command-line options from argparse or optparse

2010-05-18 Thread Knacktus

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:


<>
mode = p.parse_args() #always set to either --debug or --live

if mode.live:
recipients = ['jsm...@email.com ', 
'jane...@email.com ']

# set logging to a file
elif mode.debug:
recipients = ['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 ', 
'jane...@email.com ']

# set logging to a file
 elif mode.debug:
recipients = ['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


[Tutor] Unit testing command-line options from argparse or optparse

2010-05-18 Thread 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:

<>
mode = p.parse_args() #always set to either --debug or --live

if mode.live:
recipients = ['jsm...@email.com', 'jane...@email.com']
# set logging to a file
elif mode.debug:
recipients = ['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