[Zope-Coders] sys futzing
Martijn Faassen
faassen@vet.uu.nl
Sun, 18 Nov 2001 15:33:08 +0100
Tres Seaver wrote:
> Chris Withers wrote:
>
> >Chris McDonough wrote:
> >
> >>if __name__=='__main__':
> >> import sys
> >> sys.path.insert(0, '../../..')
> >> sys.path.insert(0, '..')
> >>
> >
> >I thought doing things like this were frowned upon now?
> >
> >Is the policy on this anywhere documented yet?
I did have a web page that nobody still seems to have linked
into the main CVS wiki, which I cannot edit:
http://dev.zope.org/CVS/ZopeTestingGuidelines
But this path insertion business is new to me too. I'll add it to this
wiki page, and I can adapt the ParsedXML tests to do this type of thing,
perhaps. Though I just ripped all that path manipulation stuff *out*
for the new release..
> Note the condition: 'sys.path' only gets munged if that
>
> module is run as a script, e.g.::
>
> $ cd lib/python/Products/FooProduct/tests
> $ python checkSomeGnarlyFeature.py
>
> For "regression tests", like the multithread tests ChrisM
> put in a separate module yesterday, assuming that the test
> will be run directly from the command line is fine.
>
> If, however, the test is a unit test, then the second path
> insertion is likely to mean that test will fail when run via
> 'testrunner', as the parent package won't be in the path
> at that point. In such a case, the appropriate fix is to
> do imports from fully-qualified package/module names.
But if it's in the __name__ == "__main__" part, then it should
okay, then? Only when importing all the unit tests from various
modules into a bigger collection of tests it shouldn't do this.
Will inserting a lot of relative ../../ work in all circumstances,
by the way? Sometimes a product won't be in lib/python/Products
but in some other INSTANCE_HOME, right? This would then fail.
Regards,
Martijn