[Tutor] Using unittest module for Test Driven Development

Alex Kleider akleider at sonic.net
Mon Aug 25 09:04:14 CEST 2014


On 2014-08-24 21:30, Danny Yoo wrote:
>>>> Is there a way that I can provide the file name command line 
>>>> parameters
>>>> to compare.py so that its get_args function can be tested?
>>> 
>>> sys.argv is writeable, or better still, provide get_args() an 
>>> optional
>>> argument to use instead of sys.argv.o
>> 
>> 
>> I don't understand what you mean by "sys.argv is writeable".
> 
> 
> Hi Alex,
> 
> I think Steven's suggestion to have get_args() take in an explicit
> args argument makes the most sense: it allows the function to be
> testable.  If the behavior of the "function" depends on something
> outside of the function, making that thing an explicit parameter
> allows you to capture it.  It makes the free variable something under
> your control.
> 
> 
> What "sys.argv is writeable" means is that you can take a sledgehammer
> approach: you can save the old value of sys.argv somewhere in another
> temporary variable, assign sys.argv to the test value, do your tests,
> and then assign the original value back to sys.argv.  But this
> approach is very fragile: anything else that depends on sys.argv can
> suddenly stop working.

Thanks, Danny; yes, as I spent more time looking over Steven's suggested 
approach
I began to understand what he meant.  I appreciate your further 
elucidation, like your
'sledge hammer' metaphor and thank you for the fragility warning. I 
expect with in such
a limited scope, the dangers are not great.
Cheers,
AlexK


More information about the Tutor mailing list