help: Unit test fixture returning the same object

Michael McCracken michael_mccracken at mac.com
Wed Aug 4 12:51:16 EDT 2004


Peter Hansen <peter at engcorp.com> wrote in message news:<nKGdnfWEjOtspo3cRVn-qg at powergate.ca>...
> Michael McCracken wrote:
> 
> > The problem is that in each test case, setUp and tearDown are called
> > as expected according to the print statements, but they don't seem to
> > have any effect after the first invocation. self.testfile is always
> > the same instance as reported by id().
> 
> What Roy said... but to prove it to yourself, if you need to,
> just implement a little class-variable-based counter in the
> File __init__ method to show that you are getting different
> objects each time.  (That is, increment a global counter
> each time you go through __init__, and store the current
> count in the instance, to be retrieved and shown when you
> are printing the ids() for the objects.)
> 
> -Peter

Thanks for the responses!
I think I'm getting pretty close to the solution, but I'd appreciate
some more help.

So, I'm still surprised that id() would point to the same object every
time, but I'm willing to believe it. However, I don't think that's the
only thing that's going on - the reason I noticed this in the first
place is that the setUp method was opening a file and populating some
lists in my File object, and those lists were accumulating objects
between test methods.

The first test sees the File as it expects, while the second one sees
a File with list member variables that have twice as many of each
object it reads in, and so forth. That's why I was willing to believe
that the same id() wasn't a fluke.

I did try the global count idea, and I did get an increasing count,
despite the same id oddity. So new instances seem to be getting
created, with the same data structures. I don't think I'm accidentally
using class variables, and that is the only thing I can think of. Any
more tips?

Thanks,
-mike



More information about the Python-list mailing list