[Tutor] Using unittest module for Test Driven Development

Danny Yoo dyoo at hashcollision.org
Mon Aug 25 06:30:48 CEST 2014


>>> 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.


More information about the Tutor mailing list