[TIP] Testing optparse with Nosetests

Chris Withers chris at simplistix.co.uk
Wed Oct 31 08:13:21 PDT 2012


On 31/10/2012 11:15, Ned Batchelder wrote:
> The problem is that parse_args() is implicitly accessing shared global
> state, sys.argv. You need to refactor your code so that you can control
> what argument list it accesses, as Dan described. You'll need to
> refactor you code down to that call, and pass the arguments to it
> explicitly.

...or you can use a testfixtures replacer to make sys.argv what you need 
it to be:

from testfixtures import Replacer

with Replacer() as r:
     r.replace('sys.argv', ['scriptname', 'foo', 'bar'])

     parse_args()

cheers,

Chris

-- 
Simplistix - Content Management, Batch Processing & Python Consulting
             - http://www.simplistix.co.uk



More information about the testing-in-python mailing list