Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Python: Python

unittest & setup

 

 

Python python RSS feed   Index | Next | Previous | View Threaded


jon at jonhaddad

Nov 3, 2009, 8:02 PM

Post #1 of 3 (196 views)
Permalink
unittest & setup

Maybe I'm doing something wrong here, definitely not the most experienced
unit tester.

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.
--
Jonathan Haddad
http://www.rustyrazorblade.com


gagsl-py2 at yahoo

Nov 3, 2009, 8:42 PM

Post #2 of 3 (175 views)
Permalink
Re: unittest & setup [In reply to]

En Wed, 04 Nov 2009 01:02:24 -0300, Jonathan Haddad <jon [at] jonhaddad>
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

--
http://mail.python.org/mailman/listinfo/python-list


goon12 at gmail

Nov 4, 2009, 6:08 AM

Post #3 of 3 (173 views)
Permalink
Re: unittest & setup [In reply to]

On Tue, Nov 3, 2009 at 11:02 PM, Jonathan Haddad <jon [at] jonhaddad> wrote:
> 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.

This post that might be worth reading, as it relates to testing with
singletons.

http://misko.hevery.com/2008/08/17/singletons-are-pathological-liars/

As is this

http://misko.hevery.com/code-reviewers-guide/
--
http://mail.python.org/mailman/listinfo/python-list

Python python RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.