unittest & setup

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Wed Nov 4 05:42:55 CET 2009


En Wed, 04 Nov 2009 01:02:24 -0300, Jonathan Haddad <jon at jonhaddad.com>  
escribió:

> I've got a class, in the constructor it loads a CSV file from disc.  I'd
> like only 1 instance of the class to be instantiated.  However, when  
> running
> multiple unit tests, multiple instances of the class are created.  What's
> the best way for me to avoid this?  It takes about a few seconds to load  
> the
> CSV file.

Use a factory function:

_instance = None
def createFoo(parameters):
   if _instance is None:
     _instance = Foo(parameters)
   return _instance

and replace all occurrences of Foo(parameters) with createFoo(parameters).  
For new-style classes, you may override the __new__ method instead.

Perhaps I didn't understand your problem correctly because this is  
unrelated to unit testing...

-- 
Gabriel Genellina




More information about the Python-list mailing list