[TIP] How to detect if you are running in "test mode"

Michael Foord fuzzyman at voidspace.org.uk
Wed Aug 4 15:21:25 PDT 2010

On 04/08/2010 22:59, Jorge Vargas wrote:
> Hi,
> I'm trying to decouple a package from it's deployed environment. In 
> order to make it testable. For this I need to change some hardcoded 
> paths currently defined at module level things in the form
> PATH_TO_SCRIPTS = '/var/lib/..../'
> in production those files must be present and the build process takes 
> care of that. However in development and testing I will like to have 
> those paths be different.
> I could override those at import time but it will mean having to 
> override the variable on each test module.
> therefore I think a better solution will be to pollute the package a 
> little with a flag to check this.
> I'm thinking of something like
> PATH_TO_SCRIPTS = '/this/value/will/never/be'
> if <running in testing>:
>     PATH_TO_SCRIPTS = '../some_checkout'
> else:
>     PATH_TO_SCRIPTS = '/var/lib/...../'
> this will be at my module.__init__.py which means everything should 
> work with either path specified as long as they contain some scripts.
> Bonus points if someone could point out how to add a third flag 
> to differentiate from development and testing.
> BTW this is with unittest2 although I think the solution should be 
> valid for all other testing tools.

In general having runtime behaviour that is different between tests and 
deployment is generally a bad idea. Hardcoding paths is also generally a 
bad idea. Perhaps you can solve both by using some form of configuration?

Michael Foord

> _______________________________________________
> testing-in-python mailing list
> testing-in-python at lists.idyll.org
> http://lists.idyll.org/listinfo/testing-in-python


READ CAREFULLY. By accepting and reading this email you agree, on behalf of your employer, to release me from all obligations and waivers arising from any and all NON-NEGOTIATED agreements, licenses, terms-of-service, shrinkwrap, clickwrap, browsewrap, confidentiality, non-disclosure, non-compete and acceptable use policies ("BOGUS AGREEMENTS") that I have entered into with your employer, its partners, licensors, agents and assigns, in perpetuity, without prejudice to my ongoing rights and privileges. You further represent that you have the authority to release me from any BOGUS AGREEMENTS on behalf of your employer.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.idyll.org/pipermail/testing-in-python/attachments/20100804/6df275f1/attachment.htm>

More information about the testing-in-python mailing list